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",
                }

            );

https://www.tutorialsteacher.com/

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. 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
  3. You are doing a great job. I would like to appreciate your work for good accuracy
    Data Science Course in Chennai

    ReplyDelete
  4. Thanks for the blog post buddy! Keep them coming... boxes uk

    ReplyDelete
  5. Great survey, I'm sure you're getting a great response. Zebra Blinds

    ReplyDelete

Topics

ADFS (1) ADO .Net (1) Ajax (1) Angular (43) Angular Js (15) ASP .Net (14) Authentication (4) Azure (3) Breeze.js (1) C# (47) CD (1) CI (2) CloudComputing (2) Coding (7) CQRS (1) CSS (2) Design_Pattern (6) DevOps (4) DI (3) Dotnet (8) DotnetCore (16) Entity Framework (2) ExpressJS (4) Html (4) IIS (1) Javascript (17) Jquery (8) Lamda (3) Linq (11) microservice (3) Mongodb (1) MVC (46) NodeJS (8) React (11) SDLC (1) Sql Server (32) SSIS (3) SSO (1) TypeScript (1) UI (1) UnitTest (1) WCF (14) Web Api (15) Web Service (1) XMl (1)

Dotnet Guru Archives