The most comprehensive ASP.NET MVC routing configuration in history

巴扎黑
Release: 2017-04-09 11:15:11
Original
5348 people have browsed it

Let’s first talk about the basic routing rule principles. The basic routing rules are arranged from special to general, that is, the most special (non-mainstream) rules are at the front, and the most general (one-size-fits-all) rules are at the end. This is because matching routing rules also follow this order. If you write it backwards, even if you write the routing rules correctly, you will still wait for 404.

XD First let’s talk about the structure of the URL. In fact, this is not a structure, just a grammatical feature.

URL construction

Named parameter specification + anonymous object

routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Copy after login

Construct the route and add

Route myRoute = new Route("{controller}/{action}", new MvcRouteHandler());
routes.Add("MyRoute", myRoute);
Copy after login

Direct method overloading + anonymous object

routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
Copy after login

Personally, I think the first one is easier to understand, the second one is easier to debug, and the third one is more efficient to write. Take what you need. The writing of this article is biased towards the third type.

Routing rules

1.Default routing (comes with MVC)

routes.MapRoute(
"Default", // 路由名称
"{controller}/{action}/{id}", // 带有参数的 URL
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 (UrlParameter.Optional-可选的意思) );
Copy after login

2. Static URL segment

routes.MapRoute("ShopSchema2", "Shop/OldAction", new { controller = "Home", action = "Index" });
 
routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
routes.MapRoute("ShopSchema2", "Shop/OldAction.js",
 new { controller = "Home", action = "Index" });
Copy after login

Without placeholder routing, it is ready-made and hard-coded.

For example, if you write like this and then visit http://localhost:XXX/Shop/OldAction.js, the response will be no problem. The three reserved words controller, action, and area should not be placed in static variables.

3. Customize the regular variable URL segment (well, this translation reveals your IQ)

routes.MapRoute("MyRoute2", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "DefaultId" });
Copy after login

In this case, if you access /Home/Index, because the third segment (id) has no value, this parameter will be set to DefaultId

according to the routing rules. This can be clearly seen by using viewbag to assign a value to the title

ViewBag.Title = RouteData.Values["id"];
Copy after login

The picture is no longer posted, and the result is that the title is displayed as DefaultId. Note that you need to assign values ​​in the controller. Assigning values ​​in the view will not compile.

4. Recall the default route

Then go back to the default route. UrlParameter.Optional is called an optional URL segment. If there is no such parameter in the route, the id will be null. According to the original article, this optional URL segment can be used to achieve a separation of concerns. It is actually not very good to directly set the parameter default value in the route just now. According to my understanding, the actual parameters are sent by the user, and all we do is define the formal parameter names. However, if you insist on assigning default values ​​to parameters, it is recommended to use syntactic sugar to write them into the action parameters. For example:

public ActionResult Index(string id = "abcd"){ViewBag.Title = RouteData.Values["id"];return View();}
Copy after login

5. Variable length routing.

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
Copy after login

Here the id and the last segment are both variable, so /Home/Index/dabdafdaf is equivalent to /Home/Index//abcdefdjldfiaeahfoeiho is equivalent to /Home/Index/All/Delete/Perm/.....

6. Cross-namespace routing

This is a reminder to remember to quote the namespace and open the IIS website otherwise it will get a 404. This is very non-mainstream and is not recommended to be messed with.

routes.MapRoute("MyRoute","{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.AdditionalControllers", "UrlsAndRoutes.Controllers" });
Copy after login

But if you write it this way, the array ranking will be in no particular order. If there are multiple matching routes, an error will be reported. Then the author proposed an improved writing method.

routes.MapRoute("AddContollerRoute","Home/{action}/{id}/{*catchall}",new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.AdditionalControllers" });
 
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional },new[] { "URLsAndRoutes.Controllers" });
Copy after login

In this way, if the first URL segment is not Home, it will be handed over to the second one for processing. Finally, you can also set that if this route is not found, it will not leave a way for subsequent routes, so it will no longer look down.

Route myRoute = routes.MapRoute("AddContollerRoute",
"Home/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new[] { "URLsAndRoutes.AdditionalControllers" });  myRoute.DataTokens["UseNamespaceFallback"] = false;
Copy after login

7. Regular expression matching routing

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
 new { controller = "Home", action = "Index", id = UrlParameter.Optional },
 new { controller = "^H.*"},
new[] { "URLsAndRoutes.Controllers"});
Copy after login

Constrain multiple URLs

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
new { controller = "^H.*", action = "^Index$|^About$"},
new[] { "URLsAndRoutes.Controllers"});
Copy after login

8. Specify the request method

routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}",
 
new { controller = "Home", action = "Index", id = UrlParameter.Optional },
 
new { controller = "^H.*", action = "Index|About", httpMethod = new HttpMethodConstraint("GET") },
 
new[] { "URLsAndRoutes.Controllers" });
Copy after login

9. WebForm support

routes.MapPageRoute("", "", "~/Default.aspx");
 
 routes.MapPageRoute("list", "Items/{action}", "~/Items/list.aspx", false, new RouteValueDictionary { { "action", "all" } });
 
 routes.MapPageRoute("show", "Show/{action}", "~/show.aspx", false, new RouteValueDictionary { { "action", "all" } });
 
 routes.MapPageRoute("edit", "Edit/{id}", "~/edit.aspx", false, new RouteValueDictionary { { "id", "1" } }, new RouteValueDictionary { { "id", @"\d+" } });
Copy after login

For details, please see

Create WebForm application using Asp.Net4 new feature routing

Or official msdn

10.MVC5’s RouteAttribute

First, go to the route registration method

//启用路由特性映射
routes.MapMvcAttributeRoutes();
Copy after login

This way

[Route("Login")]
Copy after login

The route feature is only effective. This feature has several overloads. There are also routing constraints, order, route names, etc.

其他的还有路由前缀,路由默认值

[RoutePrefix("reviews")]<br>[Route("{action=index}")]<br>public class ReviewsController : Controller<br>{<br>}
Copy after login

路由构造

// eg: /users/5
[Route("users/{id:int}"]
public ActionResult GetUserById(int id) { ... }
 
// eg: users/ken
[Route("users/{name}"]
public ActionResult GetUserByName(string name) { ... }
Copy after login

参数限制

// eg: /users/5
// but not /users/10000000000 because it is larger than int.MaxValue,
// and not /users/0 because of the min(1) constraint.
[Route("users/{id:int:min(1)}")]
public ActionResult GetUserById(int id) { ... }
Copy after login
Constraint Description Example
alpha Matches uppercase or lowercase Latin alphabet characters (a-z, A-Z) {x:alpha}
bool Matches a Boolean value. {x:bool}
datetime Matches a DateTime value. {x:datetime}
decimal Matches a decimal value. {x:decimal}
double Matches a 64-bit floating-point value. {x:double}
float Matches a 32-bit floating-point value. {x:float}
guid Matches a GUID value. {x:guid}
int Matches a 32-bit integer value. {x:int}
length Matches a string with the specified length or within a specified range of lengths. {x:length(6)} {x:length(1,20)}
long Matches a 64-bit integer value. {x:long}
max Matches an integer with a maximum value. {x:max(10)}
maxlength Matches a string with a maximum length. {x:maxlength(10)}
min Matches an integer with a minimum value. {x:min(10)}
minlength Matches a string with a minimum length. {x:minlength(10)}
range Matches an integer within a range of values. {x:range(10,50)}
regex Matches a regular expression. {x:regex(^\d{3}-\d{3}-\d{4}$)}

具体的可以参考

Attribute Routing in ASP.NET MVC 5

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

11.最后还是不爽的话自己写个类实现 IRouteConstraint的匹配方法。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Routing;
/// <summary>
/// If the standard constraints are not sufficient for your needs, you can define your own custom constraints by implementing the IRouteConstraint interface.
/// </summary>
public class UserAgentConstraint : IRouteConstraint
{
 
    private string requiredUserAgent;
    public UserAgentConstraint(string agentParam)
    {
        requiredUserAgent = agentParam;
    }
    public bool Match(HttpContextBase httpContext, Route route, string parameterName,
    RouteValueDictionary values, RouteDirection routeDirection)
    {
        return httpContext.Request.UserAgent != null &&
        httpContext.Request.UserAgent.Contains(requiredUserAgent);
    }
}
Copy after login
routes.MapRoute("ChromeRoute", "{*catchall}",
 
new { controller = "Home", action = "Index" },
 
new { customConstraint = new UserAgentConstraint("Chrome") },
 
new[] { "UrlsAndRoutes.AdditionalControllers" });
Copy after login

比如这个就用来匹配是否是用谷歌浏览器访问网页的。

12.访问本地文档

routes.RouteExistingFiles = true;
 
routes.MapRoute("DiskFile", "Content/StaticContent.html", new { controller = "Customer", action = "List", });
Copy after login

浏览网站,以开启 IIS Express,然后点显示所有应用程序-点击网站名称-配置(applicationhost.config)-搜索UrlRoutingModule节点

<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="managedHandler,runtimeVersionv4.0" />
Copy after login

把这个节点里的preCondition删除,变成

<add name="UrlRoutingModule-4.0" type="System.Web.Routing.UrlRoutingModule" preCondition="" />
Copy after login

13.直接访问本地资源,绕过了路由系统

routes.IgnoreRoute("Content/{filename}.html");
Copy after login

文件名还可以用 {filename}占位符。

IgnoreRoute方法是RouteCollection里面StopRoutingHandler类的一个实例。路由系统通过硬-编码识别这个Handler。如果这个规则匹配的话,后面的规则都无效了。 这也就是默认的路由里面routes.IgnoreRoute("{resource}.axd/{*pathInfo}");写最前面的原因。

路由测试(在测试项目的基础上,要装moq)

PM> Install-Package Moq
Copy after login
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using System.Web;
using Moq;
using System.Web.Routing;
using System.Reflection;
[TestClass]
public class RoutesTest
{
    private HttpContextBase CreateHttpContext(string targetUrl = null, string HttpMethod = "GET")
    {
        // create the mock request
        Mock<HttpRequestBase> mockRequest = new Mock<HttpRequestBase>();
        mockRequest.Setup(m => m.AppRelativeCurrentExecutionFilePath)
        .Returns(targetUrl);
        mockRequest.Setup(m => m.HttpMethod).Returns(HttpMethod);
        // create the mock response
        Mock<HttpResponseBase> mockResponse = new Mock<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 = new Mock<HttpContextBase>();
        mockContext.Setup(m => m.Request).Returns(mockRequest.Object);
        mockContext.Setup(m => m.Response).Returns(mockResponse.Object);
        // return the mocked context
        return mockContext.Object;
    }
 
    private void TestRouteMatch(string url, string controller, string action, object routeProperties = null, string httpMethod = "GET")
    {
        // Arrange
        RouteCollection routes = new RouteCollection();
        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));
    }
 
    private bool TestIncomingRouteResult(RouteData routeResult, string controller, string action, object propertySet = null)
    {
        Func<object, object, bool> valCompare = (v1, v2) =>
        {
            return StringComparer.InvariantCultureIgnoreCase
            .Compare(v1, v2) == 0;
        };
        bool result = valCompare(routeResult.Values["controller"], controller)
        && valCompare(routeResult.Values["action"], action);
        if (propertySet != null)
        {
            PropertyInfo[] propInfo = propertySet.GetType().GetProperties();
            foreach (PropertyInfo pi in propInfo)
            {
                if (!(routeResult.Values.ContainsKey(pi.Name)
                && valCompare(routeResult.Values[pi.Name],
                pi.GetValue(propertySet, null))))
                {
                    result = false;
                    break;
                }
            }
        }
        return result;
    }
 
    private void TestRouteFail(string url)
    {
        // Arrange
        RouteCollection routes = new RouteCollection();
        RouteConfig.RegisterRoutes(routes);
        // Act - process the route
        RouteData result = routes.GetRouteData(CreateHttpContext(url));
        // Assert
        Assert.IsTrue(result == null || result.Route == null);
    }
 
    [TestMethod]
    public void TestIncomingRoutes()
    {
        // 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" });
    }
  
}
Copy after login

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

  哎没办法啊,技术差距就是这样了。

The above is the detailed content of The most comprehensive ASP.NET MVC routing configuration in history. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template