简体   繁体   English

如何将请求路由到MVC2中特定控制器中的特定操作?

[英]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. 在第二个路由中,lan​​gId是必需的,但这里有一个正则表达式,以便不会干扰页面的其他路由。 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"吗?

What you need is a custom route handler Have a look at this 您需要的是自定义路由处理程序,看看这个

regards. 问候。

暂无
暂无

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

相关问题 C#,ASP.NET MVC / MVC2:为特定控制器添加通配符路由-帮助构建路由 - C#, ASP.NET MVC / MVC2: Add wildcard route for specific controller - help building route 如何将特定的子域请求重新路由到其他ASP.NET MVC控制器? - How can I re-route a specific subdomain request to a different ASP.NET MVC controller? 由于我为控制器的特定操作编写了路由,因此是否需要为控制器内所有操作编写路由? - Since I wrote a route for a specific Action of a Controller, do I need to write a route for all Actions inside the Controller? 如何为特定路由Asp.Net MVC构建我的操作方法 - How do I construct my action method for a specific route Asp.Net MVC 如何将请求动态路由到特定的 controller 并使用原始请求路径作为参数进行操作 - How to dynamically route request to a specific controller and action with the original request paths as parameters 如何在ASP.NET MVC3中为特定的控制器和操作创建URL路由 - How to create url route for specific Controller and Action in asp.net mvc3 如何排除需要验证的特定控制器操作? - How do I exclude a specific controller action from needing authentication? 创建到特定操作的不同路由mvc 6 - Creating a different route to a specific action mvc 6 如何重定向到特定的动作路线 - How to Redirect to specific action route 如何在MVC4中禁用特定操作\\控制器的全局属性 - How to disable global attribute for a specific action\controller in MVC4
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM