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.
我最近似乎遇到的一个主要障碍是围绕我一直在开发的一些基于MVC的应用程序的一些更复杂的路由要求。我在找到正确的教程集时遇到问题,让我通过它来获得完整的理解。
What I'd like to find is a complete set of tutorials for everything routing from basic (controller/action/id) to advanced.
我想要找到的是一套完整的教程,用于从基本(控制器/动作/ id)到高级的所有路由。
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 / year / month / day / title - 将映射到控制器:博客和操作:发布和作为参数:年,月,日和标题
/blog/title
- would map to controller: blog
and action: post
and as parameters: title
/ blog / title - 将映射到控制器:博客和操作:发布和作为参数:标题
/title
- would map to controller: blog
and action: post
and as parameters: title
/ 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 个解决方案
#1
15
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
.
我不明白,为什么不能将它们中的每一个定义为单独的路径,在需要时使用正则表达式。例如,区分/ blog / year / month / day / title和/ 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:
这些集合中的每一个都是一个单独的案例,您需要告诉MVC如何处理每个集合。您可以通过在Global.asax.cs文件中定义规则一次来执行此操作:
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
对于第二种情况:/ blog / title
routes.MapRoute(
"Blog Title Route", // Route name
"blog/{title}", // URL with parameters
new {controller = "blog", action = "post"}, // Defaults
);
For last case: /title
对于最后一种情况:/ 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.
诀窍是将这些路线按照这个确切的顺序排列,底部最不具体。更改顺序将导致使用错误的路由(特别是在最后两个中)。如果使用第二种情况切换最后一种情况,则blog / SomeTitle类型的URL将路由到以博客作为标题的帖子操作。
Whenever you're creating a route for something, keep the following in mind:
无论何时为某事创建路线,请记住以下几点:
- Constraint route parameters with RegEx
- 使用RegEx约束路由参数
- Be
very
aware of route order (which route comes before which) - 非常了解路线顺序(之前的路线)
- The squiggly brackets
{something}
denote action parameters - 波浪形括号{something}表示动作参数
Some good tutorials:
一些很好的教程:
- http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-24-cs.aspx
- 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/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
- http://blogs.microsoft.co.il/blogs/bursteg/archive/2008/12/21/anatomy-of-an-asp-net-mvc-application.aspx
#1
15
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
.
我不明白,为什么不能将它们中的每一个定义为单独的路径,在需要时使用正则表达式。例如,区分/ blog / year / month / day / title和/ 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:
这些集合中的每一个都是一个单独的案例,您需要告诉MVC如何处理每个集合。您可以通过在Global.asax.cs文件中定义规则一次来执行此操作:
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
对于第二种情况:/ blog / title
routes.MapRoute(
"Blog Title Route", // Route name
"blog/{title}", // URL with parameters
new {controller = "blog", action = "post"}, // Defaults
);
For last case: /title
对于最后一种情况:/ 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.
诀窍是将这些路线按照这个确切的顺序排列,底部最不具体。更改顺序将导致使用错误的路由(特别是在最后两个中)。如果使用第二种情况切换最后一种情况,则blog / SomeTitle类型的URL将路由到以博客作为标题的帖子操作。
Whenever you're creating a route for something, keep the following in mind:
无论何时为某事创建路线,请记住以下几点:
- Constraint route parameters with RegEx
- 使用RegEx约束路由参数
- Be
very
aware of route order (which route comes before which) - 非常了解路线顺序(之前的路线)
- The squiggly brackets
{something}
denote action parameters - 波浪形括号{something}表示动作参数
Some good tutorials:
一些很好的教程:
- http://www.asp.net/(S(pdfrohu0ajmwt445fanvj2r3))/learn/mvc/tutorial-24-cs.aspx
- 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/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
- http://blogs.microsoft.co.il/blogs/bursteg/archive/2008/12/21/anatomy-of-an-asp-net-mvc-application.aspx