简体   繁体   中英

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 . I want to achieve the following: when a user tries to open Account/Payments/NewPayment AccountController.ExecuteNewPayment() method should be invoked.

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

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.

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". In second route langId is necessary but a regex is there so that other routes of your page are not disturbed. Without this regex, langid can be anything else.

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.

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? If MVC did use the default for langId then the first route would always get used since it was declared before the other one. 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. 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

{language}没有对应的默认值, langid应该是language = @"default"吗?

What you need is a custom route handler Have a look at this

regards.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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