简体   繁体   English

为什么需要在ASP.NET MVC中的RoutesTable的默认路由中指定控制器和动作?

[英]Why do you need to specify the controller and action in the default route for the RoutesTable in ASP.NET MVC?

Why do we have to specify the defaults for the default route? 为什么我们必须为默认路由指定默认值?

This is a normal default route: 这是正常的默认路由:

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

Why can't I just do this: 我为什么不能这样做:

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

I already specified the action and controller but when I use this way, I get an error. 我已经指定了动作和控制器,但是当我使用这种方式时,出现错误。 Does anyone know why you have to specify the action and controller in the default route? 有谁知道您为什么必须在default路由中指定动作和控制器?

Without a default set of parameters, how is routing supposed to know where to send this URL? 如果没有默认参数集,路由应该如何知道该URL的发送位置?

/

The defaults let you do that URL, so it knows to use the 'Home' controller's 'Index' method. 默认值允许您执行该URL,因此它知道使用“ Home”控制器的“ Index”方法。

Or: 要么:

/Articles

In this case, the 'Index' action of the 'Articles' controller would be called. 在这种情况下,将调用“文章”控制器的“索引”操作。 Without those defaults, again, routing has no way to know what to do. 同样,如果没有这些默认值,路由将无法知道该怎么做。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM