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

Task Parallel Library (TPL) and Akka.NET: Differences

Task Parallel Library (TPL) and Akka.NET are both powerful tools in the .NET ecosystem for handling parallelism and concurrency, but they serve different purposes and use different models of computation. Here are some key differences:s 1.    Actor Model vs Task-Based Model: Akka.NET is built around the actor model, where actors are the fundamental units of computation and they communicate by exchanging messages. TPL, on the other hand, is task-based. It's designed to make developers more productive by simplifying the process of adding parallelism and concurrency to applications. TPL uses tasks (which are independently executing work units) and provides various ways to control and coordinate them. 2.    Fault Tolerance: One of the key features of Akka.NET is its built-in fault tolerance. It has a "let-it-crash" philosophy, where the system is designed to self-heal from errors. If an actor fails, its parent actor can decide on the supervision strategy: either to resta

Extension Methods - Advanced

Here we will see how can we use the Extension Methods in advanced manner in other types Imagine you often need to retrieve items from a List based on a custom criterion that the built-in LINQ methods don't cover. Extension Methods for Lists: Filtering based on Custom Criteria And the output would be   Extending Enums: Displaying Descriptive Strings Output: Extending DateTime: Calculating Age     Output: The code samples can be found at https://github.com/oneananda/C_Sharp_Examples/tree/main/ExtensionMethods