Creating Custom Routes in asp.net mvc3

that you are building a blog application. You might want to handle incoming requests that look like this:
/Archive/12-25-2009
When a user enters this request, you want to return the blog entry that corresponds to the date 12/25/2009. In order to handle this type of request, you need to create a custom route.
The Global.asax file in Listing 1 contains a new custom route, named Blog, which handles requests that look like /Archive/entry date

In the Global.asmx file
using System.Web.Mvc;
using System.Web.Routing;
 namespace MvcApplication1
 {
public class MvcApplication : System.Web.HttpApplication
 {
public static void RegisterRoutes(RouteCollection routes)
 {
 routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute( "Blog", // Route name "Archive/{entryDate}", // URL with parameters new { controller = "Archive", action = "Entry" } // Parameter defaults );


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);
}
 }
}
Ebook Download
View all
Learn
View all