ASP.NET MVC路由配置

先说一下基本的路由规则原则。基本的路由规则是从特殊到一般排列,也就是最特殊(非主流)的规则在最前面,最一般(万金油)的规则排在最后。这是因为匹配路由规则也是照着这个顺序的。如果写反了,那么即便你路由规则写对了那照样坐等404.

XD 首先说URL的构造。 其实这个也谈不上构造,只是语法特性吧
在这里插入图片描述
在这里插入图片描述在这里插入图片描述
没有占位符路由就是现成的写死的。

比如这样写然后去访问http://localhost:XXX/Shop/OldAction.js,response也是完全没问题的。 controller , action , area这三个保留字就别设静态变量里面了。
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述
具体的可以参考

Attribute Routing in ASP.NET MVC 5

对我来说,这样的好处是分散了路由规则的定义.有人喜欢集中,我个人比较喜欢这种灵活的处理.因为这个action定义好后,我不需要跑到配置那里定义对应的路由规则
在这里插入图片描述
在这里插入图片描述
在这里插入图片描述

using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web;
using Moq;
using System.Web.Routing;
using System.Reflection;
[TestClass]
 public class RoutesTest 
{
privateHttpContextBase CreateHttpContext(stringtargetUrl = null,stringHttpMethod = "GET")
{
    // create the mock request

    Mock<HttpRequestBase> mockRequest = newMock<HttpRequestBase>();

    mockRequest.Setup(m => m.AppRelativeCurrentExecutionFilePath)

    .Returns(targetUrl);

    mockRequest.Setup(m => m.HttpMethod).Returns(HttpMethod);

    // create the mock response

    Mock<HttpResponseBase> mockResponse = newMock<HttpResponseBase>();

    mockResponse.Setup(m => m.ApplyAppPathModifier(

    It.IsAny<string>())).Returns<string>(s => s);

    // create the mock context, using the request and response

    Mock<HttpContextBase> mockContext = newMock<HttpContextBase>();

    mockContext.Setup(m => m.Request).Returns(mockRequest.Object);

    mockContext.Setup(m => m.Response).Returns(mockResponse.Object);

    // return the mocked context

    returnmockContext.Object;
}
privatevoidTestRouteMatch(stringurl,stringcontroller,stringaction,objectrouteProperties = null,stringhttpMethod = "GET")
{
     // Arrange
    RouteCollection routes = newRouteCollection();
    RouteConfig.RegisterRoutes(routes);
    // Act - process the route
    RouteData result = routes.GetRouteData(CreateHttpContext(url, httpMethod));
    // Assert
    Assert.IsNotNull(result);
    Assert.IsTrue(TestIncomingRouteResult(result, controller, action, routeProperties));
}

privateboolTestIncomingRouteResult(RouteData routeResult, stringcontroller,stringaction,objectpropertySet = null)
{
    Func<object,object,bool> valCompare = (v1, v2) =>
    {
        returnStringComparer.InvariantCultureIgnoreCase
        .Compare(v1, v2) == 0;
    };

    boolresult = valCompare(routeResult.Values["controller"], controller)
    && valCompare(routeResult.Values["action"], action);
    if(propertySet != null)
    {
        PropertyInfo[] propInfo = propertySet.GetType().GetProperties();
        foreach(PropertyInfo pi inpropInfo)
        {
            if(!(routeResult.Values.ContainsKey(pi.Name)
            && valCompare(routeResult.Values[pi.Name],
            pi.GetValue(propertySet,null))))
            {
                result = false;
                break;
            }
        }
    }
    returnresult;
}

privatevoidTestRouteFail(stringurl)
{
    // Arrange
    RouteCollection routes = newRouteCollection();
    RouteConfig.RegisterRoutes(routes);
    // Act - process the route
    RouteData result = routes.GetRouteData(CreateHttpContext(url));
    // Assert
    Assert.IsTrue(result == null|| result.Route == null);
}

[TestMethod]
publicvoidTestIncomingRoutes()
{

    // check for the URL that we hope to receive

    TestRouteMatch("~/Admin/Index","Admin","Index");

    // check that the values are being obtained from the segments

    TestRouteMatch("~/One/Two","One","Two");

    // ensure that too many or too few segments fails to match

    TestRouteFail("~/Admin/Index/Segment");//失败

    TestRouteFail("~/Admin");//失败

    TestRouteMatch("~/","Home","Index");

    TestRouteMatch("~/Customer","Customer","Index");

    TestRouteMatch("~/Customer/List","Customer","List");

    TestRouteFail("~/Customer/List/All");//失败

    TestRouteMatch("~/Customer/List/All","Customer","List",new{ id = "All"});

    TestRouteMatch("~/Customer/List/All/Delete","Customer","List",new{ id = "All", catchall = "Delete"});

    TestRouteMatch("~/Customer/List/All/Delete/Perm","Customer","List",new{ id = "All", catchall = "Delete/Perm"});
    }
}

最后还是再推荐一下Adam Freeman写的apress.pro.asp.net.mvc.4这本书。稍微熟悉MVC的从第二部分开始读好了。前面都是入门(对我来说是扯淡)。但总比国内某些写书的人好吧——把个开源项目的源代码下载下来帖到书上面来,然后标题起个深入解析XXXX,然后净瞎扯淡。最后一千多页的巨著又诞生了。Adam Freeman的风格我就很喜欢,都是实例写作,然后还在那边书里面专门写了大量的测试。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值