Click here to Skip to main content
15,896,118 members
Articles / Web Development / ASP.NET

Custom Routes for MVC Application

Rate me:
Please Sign up or sign in to vote.
4.63/5 (8 votes)
13 Dec 2011CPOL4 min read 110K   1.7K   34  
Provides details about ASP.NET MVC Routing architecture and customize structure of the URLs of your ASP.NET MVC applications.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;

namespace CustomRoutes
{
    // Note: For instructions on enabling IIS6 or IIS7 classic mode, 
    // visit http://go.microsoft.com/?LinkId=9394801

    public class MvcApplication : System.Web.HttpApplication
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");


            //Simple Custom Route with out any Constraints
            routes.MapRoute(
                "BlogWithoutConstraint",
                "Archive/{entrydate}",
                new { Controller = "Blog", action = "Archive", });


            //Custom Route With Regular Expression Constraints
            routes.MapRoute(
                "Blog",
                "Archive/{entrydate}",
                new { Controller = "Blog", action = "Archive" }, new { entryDate = @"\d{2}-\d{2}-\d{4}" });


            //Custom Route With HttpMethod Constraint
            routes.MapRoute(
                "HttpMethodConstraintRoute",
                "Blog/Insert",
                new { Controller = "Blog", action = "Insert" }, new { method = new HttpMethodConstraint("GET") });


            //Custom Route Catch-All  Routes
            routes.MapRoute(
                "CatchAllRoute",
                "CatchAll/{*AllValues}",
                new { Controller = "CatchAll", action = "Index" });


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



        }

        protected void Application_Start()
        {
            AreaRegistration.RegisterAllAreas();

            RegisterRoutes(RouteTable.Routes);

            //Uncomment Below Code When you want to Debug the Route
            RouteDebug.RouteDebugger.RewriteRoutesForTesting(RouteTable.Routes);
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer
India India
I have been working as a Software Engineer on Microsoft .NET Technology.I have developed several web/desktop application build on .NET technology .My point of interest is Web Development,Desktop Development,Ajax,Json,Jquey,XML etc.I have completed Master of Computer Application in May-2011.I'm not happy unless I'm learning something new.

Comments and Discussions