Monday, 19 November 2018

Custom Routes for MVC Application

When you request any page into MVC Application, it will go through the Routing Architecture. Your Routing system will decide your URL pattern. The default routing algorithm is like {controller}/ {action}/ {id} patterns. But it can be possible to change that pattern using Custom Routes.

Default Route
You can configure Route into the Global.aspx's Application Start Event. When you are creating an MVC application, it will already define Default route. Default route can be configured using the following code:

      name: "Default",
      url: "{controller}/{action}/{id}",
      defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional });

Custom Routes
It is possible to develop an MVC application without creating a Custom Route. But sometimes, there is a situation to change the pattern of URL in which it makes sense to create Custom Routes.

Examples of Valid Route Patterns in ASP.NET MVC

Route PatternURL Example

Suppose I have a BlogController like below.

public class BlogController : Controller
        public string Archive(DateTime? entryDate)
            return "You wants Blog Entry on Date:=" + entryDate.ToString();

        public string All()
            return "All function";

        public string getData(string name, int empId)
            return "name: " + name + " id= " + empId;

1- Imagine that you want to route a request look like: /Archive/12-25-2010.


                            new { Controller = "Blog", action = "Archive", });

2- Imagine that you want to route a request look like: /MyBlogs/All/12 or /MyBlogs/All/

            // MyBlogs/All/12  
            // MyBlogs/All/
                   name: "myBlogRoute2",
                   url: "MyBlogs/{action}/{id}",
                   defaults: new
                       controller = "Blog",
                       action = "All",
                       id = UrlParameter.Optional


3- Imagine that you want to route a request look like: /MyBlogs/Suraj/Post/101

            // MyBlogs/suraj/Post/101
                name: "myBlogRoute3",
                url: "MyBlogs/{name}/Post/{postId}",
                defaults: new
                    controller = "Blog",
                    action = "getData",



  1. Nice post. By reading your blog, i get inspired and this provides some useful information. Thank you for posting this exclusive post for our vision. 

    Java training in Bangalore | Java training in Marathahalli

    Java training in Bangalore | Java training in Btm layout

    Java training in Bangalore |Java training in Rajaji nagar

  2. Great Article… I love to read your articles because your writing style is too good, its is very very helpful for all of us and I never get bored while reading your article because, they are becomes a more and more interesting from the starting lines until the end.

    rpa training in chennai
    Best rpa training in bangalore
    rpa course in bangalore
    rpa training in marathahalli
    rpa training in btm
    best rpa training in chennai

  3. Thank you a lot for providing individuals with a very spectacular possibility to read critical reviews from this site.
    Best Devops training in sholinganallur
    Devops training in velachery
    Devops training in annanagar
    Devops training in tambaram


  4. Whoa! I’m enjoying the template/theme of this website. It’s simple, yet effective. A lot of times it’s very hard to get that “perfect balance” between superb usability and visual appeal. I must say you’ve done a very good job with this.


  5. You are doing a great job. I would like to appreciate your work for good accuracy
    Data Science Course in Chennai

  6. TreasureBox is operated by a group of young, passionate, and ambitious people that are working diligently towards the same goal - make your every dollar count, as we believe you deserve something better.
    Check out the best
    entertainment unit



ADFS (1) ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Authentication (1) Azure (1) Breeze.js (1) C# (50) CD (1) CI (2) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DevOps (4) DI (4) Dotnet (22) Entity Framework (3) ExpressJS (4) Html (3) IIS (1) Javascript (6) Jquery (8) Lamda (3) Linq (11) Mongodb (1) MVC (50) NodeJS (7) RDLC (1) Report (1) SDLC (1) Sql Server (30) SSIS (3) SSO (1) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)