Advanced ASP Routing tutorials and examples

谁说胖子不能爱 提交于 2019-12-03 09:38:45

问题


One of major hurdles I seem to be having recently is getting my head around some of the more complex routing requirements for some MVC based applications I've been developing. I'm having problems finding the right set of tutorials to walk me through it to get a complete understanding.

What I'd like to find is a complete set of tutorials for everything routing from basic (controller/action/id) to advanced.

An example of what I'm calling advanced routing is things like:

/blog/year/month/day/title - would map to controller: blog and action: post and as parameters: year, month, day and title

/blog/title - would map to controller: blog and action: post and as parameters: title

/title - would map to controller: blog and action: post and as parameters: title

I could map each possible set to an explicit route in global using a database, but that seems like it's defeating the point of having the routing engine route to the correct place. I'd rather define the rule once.


回答1:


I don't understand, why can't you just define each one of them as a separate route, using regular expression when needed. For example to differentiate between the /blog/year/month/day/title and /blog/title.

Each one of those sets is a separate case, and you'll need to tell MVC what to do with each one. You can do this by defining the rule once in the Global.asax.cs file:

For the first case: /blog/year/month/day/title

routes.MapRoute(
    "Blog Full Route", // Route name
    "blog/{year}/{month}/{day}/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    new {year = @"\d+", month= @"\d+", day = @"\d+"} // Constrain parameters with RegEx patterns
    );

For second case: /blog/title

routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );

For last case: /title

routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );

The trick is putting theses routes in this exact order, with the least specific at the bottom. Changing the order would result in the wrong route being used (specifically in the last two). If the last case was switched with the second case, URLS of the type blog/SomeTitle would route to the post action with blog as the title.

Whenever you're creating a route for something, keep the following in mind:

  1. Constraint route parameters with RegEx
  2. Be very aware of route order (which route comes before which)
  3. The squiggly brackets {something} denote action parameters

Some good tutorials:

  • http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-24-cs.aspx
  • http://blogs.microsoft.co.il/blogs/bursteg/archive/2009/01/11/asp-net-mvc-route-constraints.aspx
  • http://blogs.microsoft.co.il/blogs/bursteg/archive/2008/12/21/anatomy-of-an-asp-net-mvc-application.aspx


来源:https://stackoverflow.com/questions/2341057/advanced-asp-routing-tutorials-and-examples

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!