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:

     routes.MapRoute(
      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
mysite/{username}/{action}~/mysite/jatten/login
public/blog/{controller}-{action}/{postId}~/public/blog/posts-show/123
{country}-{lang}/{controller}/{action}/{id}~/us-en/products/show/123
products/buy/{productId}-{productName}~/products/but/2145-widgets

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.

          routes.MapRoute(
                            "myBlogRoute1",
                            "{Archive}/{entrydate}",

                            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/
            routes.MapRoute(
                   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
            routes.MapRoute(
                name: "myBlogRoute3",
                url: "MyBlogs/{name}/Post/{postId}",
                defaults: new
                {
                    controller = "Blog",
                    action = "getData",
                }

            );


13 comments:

  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

    ReplyDelete
  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

    ReplyDelete
  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

    ReplyDelete

  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.

    AWS TRAINING IN BANGALORE|NO.1AWS TRAINING INSTITUTES IN BANGALORE

    ReplyDelete

Topics

ADO .Net (2) Ajax (1) Angular (1) Angular Js (17) Angular2 (28) ASP .Net (14) Azure (1) Breeze.js (1) C# (50) CloudComputing (1) CMS (1) CSS (2) Design_Pattern (3) DI (3) 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) SSRS (2) UI (1) WCF (13) Web Api (11) Web Service (1) XMl (1)