Shoaib Ijaz
Shoaib Ijaz

Reputation: 5587

How to create url route for specific Controller and Action in asp.net mvc3

I am working in Asp.net mvc3 application.I have created url for product detail page like this

routes.MapRoute(
   "ProductDetail",
   "{category}/{title}-{id}",
   new { controller = "ProductDetail", action = "Index" }
); 

for other controller using this

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

Following code creating this url

www.example.com/Shoes/blackshoes-100

Now Problem is that i want to use this url for ProductDetail page if add any other controller and action name it will redirect to ProductDetail page like this

www.example.com/Home/Index-100

How can i restrict this url for ProductDetail Page?

is this right way to do this?

I wan to hide Controller and Action of Productdetail page.

Category,title and id values are changing for every product.

Upvotes: 2

Views: 3982

Answers (1)

Łukasz W.
Łukasz W.

Reputation: 9745

You have to define routes for any other page you have and map those routes before you map your peoduct detail route. Then the route maching mechanism will find them first and use them.

Of course you do not have to map route for every single action. You can create some prefixes for example for diffrent controllers like example below, to catch all routes for Home controller actions:

routes.MapRoute(
    "Home",
    "Home/{action}/{id}",
    new { controller = "Home", action = "Index", id = "" }
);

Upvotes: 1

Related Questions