TOC
Routing:

Introduction

Routing in MVC is the concept of mapping a URL, which is what the end-user will be requesting through their browser, to a method in one of your controllers, which will then send a response back to the browser. The response will often be a View, but it can also be a simple piece of text, a Redirect or a number of other options.

Default routing

If you're working on a brand new project, created from the Empty template in Visual Studio, then your application doesn't have any routing yet. We talked a bit about routing in the article about adding a Controller to the project, but if you haven't followed this tutorial from the start, we need to check up on this. Try opening your Startup.cs file, which should have been added to your project when you created it - if there's nothing about routing, you need to add it manually. The easiest way is to use the helper method called UseMvcWithDefaultRoute() - it adds a basic route to your project, which will try to map URL's to methods on one of your controllers. If no Controller name is specified, the URL will be mapped to the HomeController.

Here's how it should look in your Startup.cs file:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    if (env.IsDevelopment())
    {
app.UseDeveloperExceptionPage();
    }

    app.UseMvcWithDefaultRoute();
}

The important part is the last line, where we call the UseMvcWithDefaultRoute() method. Thanks to this method, your application now supports basic routing. Here are some examples of URL's that now works and the Controller/Method combination that they map to:

http://localhost/           ->      HomeController.Index()  
http://localhost/Home/Index     ->      HomeController.Index()  
http://localhost/Test/  ->      HomeController.Test()  
http://localhost/Home/Something ->      HomeController.Something()

But it doesn't just affect the HomeController - instead, it adds general support to access a method on a Controller without having to create specific routes for it. So, if you have a controller called UserController, which has a method called Details(), you could access it by calling the following URL: /User/Details

In other words, the default route acts as a sort of "catch-all" rule, to make it easy for you to get some basic pages up and running. But of course, this only works for you if all your content follows the same pattern: /[name-of-controller]/[name-of-method]

As soon as you deviate from this, e.g. because you don't want the specific name of your controller to appear in the URL, you will need to add your own rules. Don't worry - you can still rely on the default routing, simply by adding your own rules before the default routing is applied. More on that later.

Summary

Routing is the process of mapping URL's, which is how a user accesses a specific page or functionality in your application, to the Controller and the method on that Controller which should be responsible for creating the response to the URL. Thanks to default routing, as we have seen in this article, you can get the most basic routing functionality very easy, but for more complex situations, you will need to create your own routes. No worries, we will talk much more about just that in the next articles.

This article has been fully translated into the following languages: Is your preferred language not on the list? Click here to help us translate this article into your language!