asp.net mvc路由与两个控制器或动作名称引用

时间:2022-06-26 17:12:26

I am not an advanced developer. I just started working with MVC. Few days back I had seen an example of ASP.NET MVC routing code where two controller or action name has been referenced.

我不是高级开发人员。我刚开始使用MVC。几天前,我看到了一个ASP.NET MVC路由代码示例,其中引用了两个控制器或操作名称。

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

Just started working with ASP.NET MVC, so I am curious to know what is the objective to mention controller or action name twice in routing code?

刚开始使用ASP.NET MVC,所以我很想知道在路由代码中两次提到控制器或动作名称的目标是什么?

In above example there are two defaults.... When and why is it required?

在上面的例子中有两个默认值....何时以及为什么需要它?

Just requesting some one to explain the same with a nice example.

只是请一个人用一个很好的例子解释同样的事情。

Thanks in advance.

提前致谢。

1 个解决方案

#1


0  

The example from the link you posted:

您发布的链接中的示例:

context.MapRoute(
    "UserHome",
    "User/{id}",
    new { action = "Index", controller = "Home", area = "User", id = 0, 
        httproute = true },
    new { controller = @"Home", id = @"\d+" }
);

is a bit confusing, because it is using anonymous types without named arguments. If you add named arguments to that example, it would look like this:

有点令人困惑,因为它使用的是没有命名参数的匿名类型。如果为该示例添加命名参数,它将如下所示:

context.MapRoute(
    name: "UserHome",
    url: "User/{id}",
    defaults: new { action = "Index", controller = "Home", area = "User", id = 0, 
        httproute = true },
    constraints: new { controller = @"Home", id = @"\d+" }
);

This makes it more clear what is going on here. The example is not showing two sets of defaults, instead there are constraints that limit the range of values that will match.

这使得更清楚的是这里发生了什么。该示例未显示两组默认值,而是存在限制将匹配的值范围的约束。

It basically says:

它基本上说:

  1. controller = @"Home" - When generating the URL, only match this route if controller route value is Home.
  2. controller = @“Home” - 生成URL时,如果控制器路由值为Home,则仅匹配此路由。

  3. id = @"\d+" - Match if the id route value contains only numerals.
  4. id = @“\ d +” - 如果id路由值仅包含数字,则匹配。

Actually, both of the above constraints run for both incoming URL matching and URL generation, but controller = @"Home" will always be true when matching the incoming URL because the default value is the only thing that can set it (and the default value is also Home).

实际上,上述两个约束都针对传入的URL匹配和URL生成运行,但是在匹配传入的URL时,controller = @“Home”将始终为true,因为默认值是唯一可以设置它的值(和默认值)也是Home)。

#1


0  

The example from the link you posted:

您发布的链接中的示例:

context.MapRoute(
    "UserHome",
    "User/{id}",
    new { action = "Index", controller = "Home", area = "User", id = 0, 
        httproute = true },
    new { controller = @"Home", id = @"\d+" }
);

is a bit confusing, because it is using anonymous types without named arguments. If you add named arguments to that example, it would look like this:

有点令人困惑,因为它使用的是没有命名参数的匿名类型。如果为该示例添加命名参数,它将如下所示:

context.MapRoute(
    name: "UserHome",
    url: "User/{id}",
    defaults: new { action = "Index", controller = "Home", area = "User", id = 0, 
        httproute = true },
    constraints: new { controller = @"Home", id = @"\d+" }
);

This makes it more clear what is going on here. The example is not showing two sets of defaults, instead there are constraints that limit the range of values that will match.

这使得更清楚的是这里发生了什么。该示例未显示两组默认值,而是存在限制将匹配的值范围的约束。

It basically says:

它基本上说:

  1. controller = @"Home" - When generating the URL, only match this route if controller route value is Home.
  2. controller = @“Home” - 生成URL时,如果控制器路由值为Home,则仅匹配此路由。

  3. id = @"\d+" - Match if the id route value contains only numerals.
  4. id = @“\ d +” - 如果id路由值仅包含数字,则匹配。

Actually, both of the above constraints run for both incoming URL matching and URL generation, but controller = @"Home" will always be true when matching the incoming URL because the default value is the only thing that can set it (and the default value is also Home).

实际上,上述两个约束都针对传入的URL匹配和URL生成运行,但是在匹配传入的URL时,controller = @“Home”将始终为true,因为默认值是唯一可以设置它的值(和默认值)也是Home)。