2

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.

1 Answer 1

1

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 = "" }
);
Sign up to request clarification or add additional context in comments.

2 Comments

yes i am using this route for other controller.but when use url like this www.example.com/Home/Index-100 . here home controller and Index action treated like category,and title
If you mean the default route like in your question then it can't work.. you have to specify such route for each controller.. also you have to remember to register this route before you will register your ProductDetails route.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.