Dynamically Routing In ASP.NET MVC With Example

ASP.NET Technology

Dynamically Routing In ASP.NET MVC With Example

Today, We want to share with you Dynamically Routing In ASP.NET MVC With Example.
In this post we will show you Customizing Routes in ASP.NET MVC, hear for ASP.NET MVC Routing Examples we will give you demo and example for implement.
In this post, we will learn about Routing In ASP.NET And ASP.NET MVC With Example with an example.

Introduction to Routing in Asp.Net MVC

Routing is a pattern matching mechanism that accepts the incoming request and decides what to do with that request. At runtime, Routing engine makes use the Route table for matching the incoming request’s URL pattern with the URL patterns defined in the C#.Net OOPS Concepts, Features & Explanation As per your needs, you can register one or more URL patterns to the Route table at Application_Start event.

Routing in Asp.Net MVC with Example

Below you can find simple routing rule that you can write inside RouteConfig.cs file.

  public class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

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

In above pakainfo I have defined the Route Pattern for Routing purpose as {controller}/{action}/{id} and also provide the default values for a controller, action and id parameters. Default value mean if you do not provide the values for controller or action or id defined in the pattern(URL) then these values will be served by the routing system.

Let us elaborate above explanation with an pakainfo so you get the better idea about how it works. Let say your website is running on www.pakainfo.com then the URL pattern for your application will be www.pakainfo.com/{controller}/{action}/{id}. as a result, you have to provide the controller name followed by action name and id if it is required. If you do not provide any of the value then default values of these parameters will be provided by the routing engine.

Below I have provided some possiblelist of URLs that match and don’t match this route pattern.

(1) Request URL : https://www.pakainfo.com/

Matching Parameter : controller=Home, action=Index, id=none, Since default value of controller and action are Home and Index respectively.

(2) https://www.pakainfo.com/Admin

Matching Parameter : controller=Admin, action=Index, id=none, Since default value of action is Index.

(3) https://www.pakainfo.com/Admin/Product

Matching Parameter : controller=Admin, action=Product, id=none

(4) https://www.pakainfo.com/Admin/Product/1

Matching Parameter : controller=Admin, action=Product, id=1

(5) https://www.pakainfo.com/Admin/Product/SubAdmin/1

Matching Parameter :No Match Found (404)

(6) https://www.pakainfo.com/Admin/Product/SubAdmin/Save/1

Matching Parameter : No Match Found (404)

Read :

Summary

You can also read about AngularJS, ASP.NET, VueJs, PHP.

I hope you get an idea about ASP.NET MVC Routing Example.
I would like to have feedback on my Pakainfo.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.


Related FAQ

Here are some more FAQ related to this Article:

  1.   Login with Facebook using javascript
  2.   Ajax Upload Multiple Images without form submitting
  3.   Simple JQuery Ajax Request Example with Laravel 6
  4.   Move Background Image on Mouseover Using jQuery
  5.   Toggle Switch Button in VueJs Example
  6.   Laravel 6 Define Global Constants Config PHP file
  7.   Laravel Image Upload Validation Tutorial
  8.   IMPLODE() AND EXPLODE() FUNCTION IN PHP
  9.   Top 10 Advanced AngularJS Interview Questions Answers
  10.   Laravel 6 concatenate Multiple Columns with Example

Leave a Reply

Your email address will not be published. Required fields are marked *