Skip to main content

ASP.NET MVC Learning 2 : Routing Basics

ASP.NET Routing in ASP.NET MVC is the concept of mapping the incoming request (URL) with the existing resources 

For Example : 

The incoming request URL for a local application is http://localhost:2233/home/index

In the Application say we have configured the routing as "{controller}/{action}/{id}", so Home is the controller here and index is the action here, the id is optional in most cases, if id is not given then it will display the default value

In ASP.NET MVC the controllers should have the naming convention, in this case the controller's name should be HomeController, should end with 'Controller' suffix.

The Routing mechanism looks in the HomeController for the Action that we set already,

Here Home is Controller and the Action is Index

In the Home Controller we will return the View (That is Index) as Action Result

Sample Home Controller
public class HomeController : Controller
{
public ActionResult Index() { return View(); }
}

You can do some minimal changes to the view in the controller like setting the header, customized message etc.



Configuring Route

Routing configuration happens in the RegisterRoutes method of RouteConfig.cs file in the App_Start folder,

    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 }
            );
        }
    }

IgnoreRoute, as the name says this will ignore the .axd files from mapping,

.axd files are static files which are not present in the disk so allowing those files may lead to problem


More details about IgnoreRoute can be found here



MapRoute is the one we are going to analyse fully

We have the following attributes,

Name --> Name of the Route

URL  --> Key part in routing
Defaults--> The default values needed to be shown if no proper URL is given.


Registering the route:

You need to register the route in the Global.asax.cs file to make available the configuration



        protected void Application_Start()
        {
            RouteConfig.RegisterRoutes(RouteTable.Routes);
        }


Key Points:

1) We can implement the routing concept for both ASP.NET Web Forms and MVC

2) We can change the URL Patterns as we want, the typical pattern most of the developers use is {controller}/{action}/{id}

3) Naming default URL pattern into 'Default' or empty string '' is permissible


ASP.NET Routing Advanced Concepts

Comments

Popular posts from this blog

Using of global variables in C# - Drawbacks & Solutions

How using global variables can have implications on the design, maintainability, and test-ability of C# code: Harder to understand and reason about the code:       class Program     {         public static int globalCounter = 0;         static void Main()         {             globalCounter++;             Console.WriteLine(globalCounter);         }     }   In this example, the global variable globalCounter is accessible from anywhere in the program, including the Main method. It's not clear where the value of the globalCounter is updated, it could be updated in other methods or classes, making it harder to trace the flow of data and understand the source of bugs.   More prone to errors:       class Program     {         public static string globalString;         static void Main()         {             globalString = "Hello" ;             Method1();             Method2();         }         static void Method1()         {

Task Parallel Library (TPL) and Akka.NET Alternatives

Task Parallel Library (TPL) and Akka.NET are among the most commonly used libraries for parallel and concurrent programming in the .NET ecosystem. However, there are also several other options available, depending on your specific needs: Parallel Language Integrated Query (PLINQ) is a parallel programming feature of .NET that provides an easy and efficient way to perform operations on collections in parallel. LINQ (Language Integrated Query) is a powerful feature in .NET that allows developers to work with data in a more declarative and language-integrated manner. While LINQ queries are inherently sequential, PLINQ extends LINQ by providing parallel versions of the query operators, allowing some queries to execute faster by utilizing multiple processors or cores on a machine. PLINQ is great when you are working with large collections where operations might be CPU-intensive or I/O-bound and could potentially be sped up by parallel execution. Here is a simple example of a PLI

SOLID Principles with Real World examples in C#

  SOLID Principles with Real World examples in C#   SOLID principles are formed by using S Single Responsibility Principles (SRP) O Open Closed Principle (OCP) L Liskov’s Substitution Principle (LCP) I Interface Segregation Principle (ISP) D Dependency Inversion Principle (DIP)   S Single Responsibility Principles (SRP) There should never be more than one reason for a class to change, to be precise one class should have only one responsibility Single Responsibility Principles (SRP) Real world example, A perfect match for SRP is Microservices , a Microservice will not contain functionalities other than the one it is designated to do,  Example ·                   Order Processing Service, ·                   Shipment Management Service, ·                   User Authentication Service, ·                   Catalogue List Service       class OrderProcessor     {         public void Process(Order order)         {             // Check inven