tags:

views:

36

answers:

3

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?

+2  A: 

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.

BritishDeveloper
That doesn't seem to be the answer. After I logon I navigate to this page; http://localhost:4275/Contract/List, but now it falls over.
arame3333
That's because you don't have an action parameter in your contract route. When you moved it up about the default, it now captured the link you commented on, but List is not a valid contractID (which I assume is an Int).
Tommy
I appreciate your advice.
arame3333
Yeah, you're missing the contractId too. you realise you can only have one action (details) in your contract controller now?!
BritishDeveloper
A: 

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 } 
        ); 
XstreamINsanity
+1  A: 

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.

Dzejms
That works, thank you. It does mean that "Details" has to be in the pathname. I am happy with that for what I am doing, but for my own education I think I would like to know how to exclude "Details" from the path and of it to work.
arame3333
For education then ;) If you wanted to exclude details from the path you would make it Contract/{contractId} (how you had before) but keep the default action="details" in (how you had before). It was the order that was tripping you up
BritishDeveloper