您认为{controller}
子字符串充当控制器名称的占位符是正确的。考虑到这一点,以下路由将匹配任何控制器,但默认为User
未指定控制器的控制器:
routes.MapRoute(
"post-User",
"{controller}",
new { controller = "User", action = "create" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
但是,以下内容将匹配路由User
,并且 - 因为无法指定控制器 - 将始终路由到User
控制器:
routes.MapRoute(
"post-User",
"User",
new { controller = "User", action = "create" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
在这种情况下,差异是没有意义的,因为您所做的只是强制路由User
映射到控制器User
,这正是您的第一条路由中会发生的情况。
但是,请考虑以下事项:
routes.MapRoute(
"post-User",
"User/{action}",
new { controller = "User", action = "MyDefaultAction" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
routes.MapRoute(
"foo",
"{controller}/{action}",
new { controller = "User", action = "Index" },
new { httpMethod = new HttpMethodConstraint("POST") }
);
现在,您的顶级路由将匹配对User
控制器的请求,并指定一个可选操作,默认为MyDefaultAction
. 对任何其他控制器的请求将不匹配第一个路由 - 因为该路由不以常量字符串开头User
- 并且将默认返回到第二个路由 (foo)。同样,该操作是可选的;但是,现在,与对User
控制器的请求不同,您对其他控制器的默认操作将是该Index
操作。
所以现在...
.../User
默认为MyDefaultAction
动作。
.../SomeOtherController
默认为Index
动作。