简体   繁体   中英

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM