Asp.net mvc 高级ASP路由教程和示例

Asp.net mvc 高级ASP路由教程和示例,asp.net-mvc,asp.net-routing,Asp.net Mvc,Asp.net Routing,我最近遇到的一个主要障碍是,我一直在开发一些基于MVC的应用程序,这些应用程序的路由要求比较复杂。 我很难找到一套正确的教程来引导我通过它来获得完整的理解 我想找到的是一套完整的教程,涵盖从基本(控制器/操作/id)到高级的所有路由 我称之为高级路由的一个例子是: /blog/year/month/day/title-将映射到控制器:blog和操作:post,并作为参数:year、month、day和title routes.MapRoute( "Blog Full Route", //

我最近遇到的一个主要障碍是,我一直在开发一些基于MVC的应用程序,这些应用程序的路由要求比较复杂。 我很难找到一套正确的教程来引导我通过它来获得完整的理解

我想找到的是一套完整的教程,涵盖从基本(控制器/操作/id)到高级的所有路由

我称之为高级路由的一个例子是:

/blog/year/month/day/title
-将映射到控制器:
blog
和操作:
post
,并作为参数:
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
    );
routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
/blog/title
-将映射到控制器:
blog
和操作:
post
,并作为参数:
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
    );
routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
/title
-将映射到控制器:
blog
和操作:
post
,并作为参数:
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
    );
routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );

我可以使用数据库将每个可能的集合映射到全局中的显式路由,但这似乎破坏了将路由引擎路由映射到正确位置的目的。我宁愿只定义一次规则。

我不明白,为什么不能在需要时使用正则表达式将它们中的每一个定义为单独的路由呢。例如,区分
/blog/year/month/day/title
/blog/title

这些集合中的每一个都是一个单独的案例,您需要告诉MVC如何处理每一个集合。您可以通过在
Global.asax.cs
文件中定义一次规则来执行此操作:

对于第一种情况:
/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
    );
routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
对于第二种情况:
/blog/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
    );
routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
对于最后一种情况:
/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
    );
routes.MapRoute(
    "Blog Title Route", // Route name
    "blog/{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
routes.MapRoute(
    "Title Route", // Route name
    "{title}", // URL with parameters
    new {controller = "blog", action = "post"},   // Defaults
    );
诀窍是将这些路线按此精确顺序排列,最不具体的在底部。更改顺序会导致使用错误的路线(特别是在最后两种情况下)。如果最后一种情况与第二种情况切换,则类型为
blog/SomeTitle
的URL将路由到
post
操作,标题为
blog

无论何时为某物创建路线,请记住以下几点:

  • 使用正则表达式约束路由参数
  • 非常了解路线顺序(哪条路线在哪条之前)
  • 弯曲的括号
    {something}
    表示动作参数
  • 一些好的教程: