开发者

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: T开发者_StackOverflow中文版he 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} has no counterpart default value, is langid supposed to be language = @"default"?


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

regards.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜