arame3333
arame3333

Reputation: 10193

MVC: Whats wrong with this routing?

In my Global.asax file I have the following;

 public static void RegisterRoutes(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?

Upvotes: 0

Views: 123

Answers (3)

BritishDeveloper
BritishDeveloper

Reputation: 13349

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.

Upvotes: 2

Dzejms
Dzejms

Reputation: 3258

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.

Upvotes: 2

XstreamINsanity
XstreamINsanity

Reputation: 4296

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 } 
        ); 

Upvotes: 0

Related Questions