开发者

MVC: Whats wrong with this routing?

In my Global.asax file I have the following;

 public static void RegisterRout开发者_JAVA百科es(RouteCollection routes)
    {
        routes.IgnoreRoute("{*favicon}", new { favicon = @"(.*/)?favicon.ico(/.*)?" });

        routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

        routes.MapRoute(
            "Default", // Route name
            "{controller}/{action}/{id}", // URL with parameters
            new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }); // Parameter defaults

        routes.MapRoute(
            "Contracts",
            "Contract/{contractId}",
            new { controller = "Contract", action = "Details" }
            );

I want to specify a route Contract/10 where 10 is the contractId which is the parameter for the Detail method in my ContractController. So this works; http://localhost:1234/Contract/Details?contractId=10 But not http://localhost:1234/Contract/10

What am I doing wrong?


Change

   routes.MapRoute(
            "Contracts",
            "Contract/{contractId}",
            new { controller = "Contract", action = "Details" }
            );

To

   routes.MapRoute(
            "Contracts",
            "Contract/{action}/{contractId}",
            new { controller = "Contract", action = "Details" }
            );

And put it before the Default route.


Put the "Default" route last after the "Contracts" route and all will be well.

The routing table finds the first match from top to bottom then stops looking. With this in mind always put more specific routes above the more generic ones

    routes.MapRoute(
        "Contracts",
        "Contract/{contractId}",
        new { controller = "Contract", action = "Details", contractId = UrlParameter.Optional }
        );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", // URL with parameters
        new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }); // Parameter defaults

Then make sure the Details action method in you ContractController accepts a parameter called contractId.


Are you saying that typing in "http://localhost:1234/Contract/Details?contractID=10" does work and "http://locatlhost:1234/Contract/10" does not work? Have you tried: "http://localhost:1234/Contract/Details/10"? Or in the "Contracts" MapRoute, put after "action = "Details"": ", contractId = UrlParameter.Optional. This way, it will look like this.

   routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

    routes.MapRoute( 
        "Default", // Route name 
        "{controller}/{action}/{id}", // URL with parameters 
        new { controller = "Account", action = "LogOn", id = UrlParameter.Optional }); // Parameter defaults 

    routes.MapRoute( 
        "Contracts", 
        "Contract/{contractId}", 
        new { controller = "Contract", action = "Details", contractId = UrlParameter.Optional } 
        ); 
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜