ASP.NET Core - Routing

Introduction

In ASP.NET Core, Routing is the process of directing an HTTP request to a Controller. Let us understand its working with an example. We have created a Controller (HomeController) in our application which is a C# class that doesn’t need to be derived from a base class, or implement an interface, or to have any special attribute. It is a plain C# class with a name, HomeController, and it contains the Index method which returns a string.

 ASP.NET Core - Routing
 
Startup.cs

  1. using Microsoft.AspNetCore.Builder;  
  2. using Microsoft.AspNetCore.Hosting;  
  3. using Microsoft.Extensions.Configuration;  
  4. using Microsoft.Extensions.DependencyInjection;  
  5. namespace RoutingSample {  
  6.     publicclassStartup {  
  7.         public Startup(IConfiguration configuration) {  
  8.             Configuration = configuration;  
  9.         }  
  10.         public IConfiguration Configuration {  
  11.             get;  
  12.         }  
  13.         // This method gets called by the runtime. Use this method to add services to the container.  
  14.         publicvoid ConfigureServices(IServiceCollection services) {  
  15.             services.AddMvc();  
  16.         }  
  17.         // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.  
  18.         publicvoid Configure(IApplicationBuilder app, IHostingEnvironment env) {  
  19.             if (env.IsDevelopment()) {  
  20.                 app.UseDeveloperExceptionPage();  
  21.             }  
  22.             app.UseMvc();  
  23.         }  
  24.     }  
  25. }  

HomeController.cs

  1. using Microsoft.AspNetCore.Mvc;  
  2. namespace RoutingSample.Controllers {  
  3.     [Route("[controller]")]  
  4.     publicclassHomeController: Controller {  
  5.         [HttpGet("Index")]  
  6.         publicstring Index() {  
  7.                 return "Hello, buddy! The message is from Home Controller.!!";  
  8.             }  
  9.             [HttpGet("State")]  
  10.         publicstring State() {  
  11.                 return "Gujarat";  
  12.             }  
  13.             [HttpGet("Country")]  
  14.         publicstring Country() {  
  15.             return "India";  
  16.         }  
  17.     }  
  18. }  
  • You can try this as well by changing the URL in the browser. In this example, it is http://localhost:44348/, except that the port might be different.

  • If you append /Home or /Home/Index to the URL and press the Enter button, you will see the same result.

Let us run the application in the browser. Once the application is run, you will see the following output.

ASP.NET Core - Routing 

In this Controller, you can see two action methods − State and Country, which will return just a State and Country name respectively. Let us save this file and specify /Home/State at the end of the root URL. 

You can see the State as in the above screenshot. If you specify /Home/Country, you will see the name of the Country too. 

Here, the ASP.NET Core MVC goes to the HomeController to specify the route of the method. You can directly access the method through routing.