johndoe
johndoe

Reputation: 207

How to make this Route work in my ASP.NET MVC Application?

I want a route something like this:

MyController/Action/categoryid/productid

So I made the following in my Global.asax file:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
      "Default", // Route name
      "{controller}/{action}/{categoryid}/{productid}",
      new { controller = "MyController", 
            action = "Action", 
            categoryid = UrlParameter.Optional, 
            productid = UrlParameter.Optional 
      } 
    );

    routes.MapRoute(
        "Default", // Route name
        "{controller}/{action}/{id}", 
        new { controller = "Home", 
              action = "Index", 
              id = UrlParameter.Optional 
        } // Parameter defaults
    );
}  

But when I browse to Mycontroller/action/2/3 the resource is not found.

Upvotes: 1

Views: 236

Answers (3)

Roger
Roger

Reputation: 1082

This is want you want:

{MyController}/{action}/{categoryid}/{productid},

But should give an more meaningfyll name to your MyController/Action part of the Url like Users/Edit/2/3

Upvotes: 0

Aliostad
Aliostad

Reputation: 81700

How does this code even build? Defining "Default" as the route name twice must stop it being compiled.

Rename one of them to something else and it must work.

It is likely that your action has other parameters or you have defined [HttpPost] as attribute and you are using GET.

Upvotes: 3

Martin Buberl
Martin Buberl

Reputation: 47164

There is a great tool from Phil Haack called ASP.NET Routing Debugger where you can type in various URLs in the address bar to see which route matches.

Upvotes: 1

Related Questions