[英]How do I route a request to a specific action in a specific controller in MVC2?
In my MVC2 application I have an AccountController
class inherited from Controller
. 在我的MVC2应用程序中,我有一个从
Controller
继承的AccountController
类。 I want to achieve the following: when a user tries to open Account/Payments/NewPayment
AccountController.ExecuteNewPayment()
method should be invoked. 我想实现以下目标:当用户尝试打开
Account/Payments/NewPayment
应调用AccountController.ExecuteNewPayment()
方法。
I added the following route: 我添加了以下路线:
routes.MapRoute(
@"CreateNewRuntime",
@"{langId}/Account/Payments/NewPayment/{whatever}",
new { langId = @"default", controller = @"Account", action = @"ExecuteNewPayment"});
but when I try to request the path above I get HTTP 404 error message with "Requested URL" /Account/Payments/NewPayment
and when I do that under debugger there's an exception 但是当我尝试请求上面的路径时,我收到带有“ Requested URL”
/Account/Payments/NewPayment
HTTP 404错误消息,当我在调试器下执行此操作时,会出现异常
A first chance exception of type 'System.Web.HttpException' occurred in System.Web.Mvc.dll Additional information: The controller for path '/Account/Payments/NewPayment' was not found or does not implement IController.
System.Web.Mvc.dll中发生类型为'System.Web.HttpException'的第一次机会异常。其他信息:找不到路径'/ Account / Payments / NewPayment'的控制器或该控制器未实现IController。
What am I doing wrong? 我究竟做错了什么? How do I perform the mapping?
如何执行映射?
I believe you should use these routes, 我相信您应该使用这些路线,
routes.MapRoute(
@"CreateNewRuntime1",
@"Account/Payments/NewPayment/{whatever}",
new { langId="en-US",controller = @"Account", action = @"ExecuteNewPayment" });
routes.MapRoute(
@"CreateNewRuntime2",
@"{langId}/Account/Payments/NewPayment/{whatever}",
new { controller = @"Account", action = @"ExecuteNewPayment" },
new { langId = "[a-z]{2}-[a-z]{2}" });
Note that here in first route, I am not using langId and setting langId default value to "en-US". 请注意,在这里,在第一条路线中,我没有使用langId并将langId默认值设置为“ en-US”。 In second route langId is necessary but a regex is there so that other routes of your page are not disturbed.
在第二个路由中,langId是必需的,但这里有一个正则表达式,以便不会干扰页面的其他路由。 Without this regex, langid can be anything else.
没有此正则表达式,langid可以是其他任何东西。
You need to include the langid
as part of your route or MVC will not map your URI to it even if you have a default specified. 您需要将
langid
作为路由的一部分包含在内,否则即使您指定了默认值,MVC也不会将URI映射到它。
Take these two routes: 采取以下两条路线:
routes.MapRoute(
@"CreateNewRuntime",
@"{langId}/Account/Payments/NewPayment/{whatever}",
new { langId = @"default", controller = @"Account", action = @"ExecuteNewPayment"});
routes.MapRoute(
@"CreateNewRuntime1",
@"{langId}/{subLangId}/Account/Payments/NewPayment/{whatever}",
new { langId = @"default", subLangId= @"test", controller = @"Account", action = @"ExecuteNewPayment1"});
If I specify the URI of /Account/Payments/NewPayment
, which one of the routes should it pick? 如果我指定
/Account/Payments/NewPayment
的URI,应该选择哪一条路由? If MVC did use the default for langId
then the first route would always get used since it was declared before the other one. 如果MVC确实对
langId
使用默认值,则第一个路由将始终被使用,因为它是在另一个路由之前声明的。 If you swapped the two then the second one would always get invoked. 如果您将两者互换,那么第二个总是会被调用。
When you have varying data in the beginning of your URI's you need to specify them and you cannot use the default value when specifying the route. 如果您在URI的开头有不同的数据,则需要指定它们,并且在指定路由时不能使用默认值。 In order for these two routes to be hit you would need a URI of
/eng/Account/Payments/NewPayment
and /eng/e/Account/Payments/NewPayment
为了使这两条路线都被击中,您需要一个URI
/eng/Account/Payments/NewPayment
和/eng/e/Account/Payments/NewPayment
{language}
没有对应的默认值, langid
应该是language = @"default"
吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.