简体   繁体   中英

URL Rewriting in Asp.net MVC 3

I am new to Asp.net MVC. I am creating web application, where i have to rewrite url with product name. I am not sure if that is possible or not in MVC.

Like, http://sitename.com/category1/product1

http://sitename.com/category1/product2

will have same page.

There are facilities to generate friendly urls within MVC.

Check out the article at - http://www.asp.net/mvc/tutorials/older-versions/controllers-and-routing/asp-net-mvc-routing-overview-cs - for an overview of how this is handled in MVC.

Essentially, you need to configure the routes on application startup as follows. This can usually be done in the global.asax file but cna be split for areas etc.

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

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

protected void Application_Start()
{
   RegisterRoutes(RouteTable.Routes); // Reigster the routes
}

This is the default route but you can define as required.

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