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.
routes.MapRoute(name: "Default",url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } );
Route myRoute = new Route("{controller}/{action}", new MvcRouteHandler()); routes.Add("MyRoute", myRoute);
routes.MapRoute("ShopSchema", "Shop/{action}", new { controller = "Home" });
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.
routes.MapRoute( "Default", // 路由名称 "{controller}/{action}/{id}", // 带有参数的 URL new { controller = "Home", action = "Index", id = UrlParameter.Optional } // 参数默认值 (UrlParameter.Optional-可选的意思) );
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" });
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.
routes.MapRoute("MyRoute2", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = "DefaultId" });
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"];
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.
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();}
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional });
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/.....
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" });
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" });
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;
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*"}, new[] { "URLsAndRoutes.Controllers"});
routes.MapRoute("MyRoute", "{controller}/{action}/{id}/{*catchall}", new { controller = "Home", action = "Index", id = UrlParameter.Optional }, new { controller = "^H.*", action = "^Index$|^About$"}, new[] { "URLsAndRoutes.Controllers"});
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" });
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+" } });
For details, please see
Create WebForm application using Asp.Net4 new feature routing
Or official msdn
First, go to the route registration method
//启用路由特性映射 routes.MapMvcAttributeRoutes();
This way
[Route("Login")]
The route feature is only effective. This feature has several overloads. There are also routing constraints, order, route names, etc.
[RoutePrefix("reviews")]
[Route("{action=index}")]
public class ReviewsController : Controller
{
}
// eg: /users/5 [Route("users/{id:int}"] public ActionResult GetUserById(int id) { ... } // eg: users/ken [Route("users/{name}"] public ActionResult GetUserByName(string name) { ... }
// 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) { ... }
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定义好后,我不需要跑到配置那里定义对应的路由规则
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Routing; ////// If the standard constraints are not sufficient for your needs, you can define your own custom constraints by implementing the IRouteConstraint interface. /// 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); } }
routes.MapRoute("ChromeRoute", "{*catchall}", new { controller = "Home", action = "Index" }, new { customConstraint = new UserAgentConstraint("Chrome") }, new[] { "UrlsAndRoutes.AdditionalControllers" });
比如这个就用来匹配是否是用谷歌浏览器访问网页的。
routes.RouteExistingFiles = true; routes.MapRoute("DiskFile", "Content/StaticContent.html", new { controller = "Customer", action = "List", });
浏览网站,以开启 IIS Express,然后点显示所有应用程序-点击网站名称-配置(applicationhost.config)-搜索UrlRoutingModule节点
把这个节点里的preCondition删除,变成
routes.IgnoreRoute("Content/{filename}.html");
文件名还可以用 {filename}占位符。
IgnoreRoute方法是RouteCollection里面StopRoutingHandler类的一个实例。路由系统通过硬-编码识别这个Handler。如果这个规则匹配的话,后面的规则都无效了。 这也就是默认的路由里面routes.IgnoreRoute("{resource}.axd/{*pathInfo}");写最前面的原因。
PM> Install-Package Moq
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 MockmockRequest = new Mock (); mockRequest.Setup(m => m.AppRelativeCurrentExecutionFilePath) .Returns(targetUrl); mockRequest.Setup(m => m.HttpMethod).Returns(HttpMethod); // create the mock response Mock mockResponse = new Mock (); mockResponse.Setup(m => m.ApplyAppPathModifier( It.IsAny ())).Returns (s => s); // create the mock context, using the request and response Mock mockContext = new Mock (); 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
最后还是再推荐一下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!