将 {Controller}/parameter 的 MVC 路由区分为 {Controller}/{action}?param=myvalue
Differentiate MVC routing for {Controller}/parameter to {Controller}/{action}?param=myvalue
我必须能够处理这样的路线:
MyController/ElementType
为此,我创建了这样的自定义路由:
context.MapRoute(
"NameOfTheRoute",
"MyPath/{controller}/{elementType}",
new { controller = "Elements", action = "Create" }
);
它工作正常,问题是当我有一条像这样的路线时
/MyPath/Elements/GetElementType?elementType=fire88
GetElementType
是一个不同的动作,但是因为我之前声明的自定义路由,所以它转到了Create
动作,我怎么知道路由是不同的动作?
之所以走这条路是因为你没有定义一个 route
来处理 action
所以 MyPath/{controller}/{elementType}
意味着在 controller
的名字之后一切都会被认为是 {elementType}
所以
您必须创建另一条路线来处理 action
routes.MapRoute(
"MyPathRouteWithAction",
"MyPath/{controller}/{action}/{elementType}",
new {controller = "Elements", action = "Create"}
);
routes.MapRoute(
"NameOfTheRoute",
"MyPath/{controller}/{elementType}",
new {controller = "Elements", action = "Create"}
);
第一个 custom route
将像 /MyPath/Elements/GetElementType/fire88
一样处理 routs
我必须能够处理这样的路线:
MyController/ElementType
为此,我创建了这样的自定义路由:
context.MapRoute(
"NameOfTheRoute",
"MyPath/{controller}/{elementType}",
new { controller = "Elements", action = "Create" }
);
它工作正常,问题是当我有一条像这样的路线时
/MyPath/Elements/GetElementType?elementType=fire88
GetElementType
是一个不同的动作,但是因为我之前声明的自定义路由,所以它转到了Create
动作,我怎么知道路由是不同的动作?
之所以走这条路是因为你没有定义一个 route
来处理 action
所以 MyPath/{controller}/{elementType}
意味着在 controller
的名字之后一切都会被认为是 {elementType}
所以
您必须创建另一条路线来处理 action
routes.MapRoute(
"MyPathRouteWithAction",
"MyPath/{controller}/{action}/{elementType}",
new {controller = "Elements", action = "Create"}
);
routes.MapRoute(
"NameOfTheRoute",
"MyPath/{controller}/{elementType}",
new {controller = "Elements", action = "Create"}
);
第一个 custom route
将像 /MyPath/Elements/GetElementType/fire88
routs