TOC

This article is currently in the process of being translated into Vietnamese (~36% done).

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

Nếu bạn tạo một dự án mới hoàn toàn từ Visual Studio, thì sẽ không có routing. Chúng ta sẽ tìm hiểu về routing. Hãy mở file Startup.cs, nếu không có thì hãy tạo ra file. Cách dễ nhất để dùng helper method là dùng UseRouting()MapDefaultControllerRoute() - nó sẽ cho chúng ta khai báo route mặc định, cho phép ánh xẹ URL vào phương thức trong Controller. Nêú không có tên Controller cụ thể thì nó sẽ ánh xạ vào HomeController.

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

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

    app.UseRouting();
    app.UseEndpoints(endpoints =>
    {
endpoints.MapDefaultControllerRoute();
    });
}

Nhờ có các phương thức này mà ứng dụng của bạn có thể hỗ trợ routing cơ bản. Dưới đây là ví dụ về URL có thể ánh xạ tới Controller/Phương thức:

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

Chúng không ảnh hưởng tới HomeController - ngoại trà nó hỗ trợ phương thức trong Controller mà không cần route cụ thể. Vậy nếu bạn có controller gọi là UserController chứa phương thức Details(), bạn có thể truy cập bằng 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!