I think you want to know about routing as a whole. It is the way to direct incoming HTTP requests to the proper methods of controller. Then as the information comes in this request a different method will be called.
The route is the path he’ll take to execute something, depending on the context, somehow gets confused with the URL. In a way we can say that it is the method within the class. The path is decided according to the HTTP verb, the various parts of the URL or some other relevant information to make a decision.
The ASP.NET MVC routing system analyzes the incoming request, searches for a pattern, and uses a criterion to decide what to do. Details about this can be set programmatically, in addition to the standard it adopts by convention according to the strings.
In older versions it used the same routing system as classic ASP.NET. In . NET Core there is a system of its own.
#Example
An example route created in the class RouteConfig
:
routes.MapRoute("Default", //nome da rota
"{controller}/{action}/{id}", // padrão do URL
new { controller = "Home", action = "Index", id = "" } // parâmetros
So what comes first at the beginning of the parameters in the URL (under normal conditions just after the domain and maybe the port) is what will determine which class controller will be called.
What comes next as a subdirectory is the action, that is, the method of that class that must be executed. But it may be that other parts are needed to determine the correct method since the methods may have the same name but different signatures, then you need to understand the type of data that will be passed if you have methods with the same name.
Finally, in this example, you find what will be passed to the method as argument. Its type could be considered in the signature.
You also set the object to default values if nothing useful to set the route.
The class that will process this would be something like this:
public class HomeController : Controller {
public ActionResult Index(string id) => View();
}
Could have a note indicating the specific verb which is accepted.
I could call it that:
dominio.com/Home/Index/101
#Attributes
It is also possible to define routes with attributes. Example:
[Route(“{produtoId:int}”)]
public ActionResult Edita(int produtoId) => View();
I put in the Github for future reference.
#Alternative to routing
All routes form a map between what comes externally and the code. Without the routes it would have to have a file for each action and the interpretation of the arguments contained in the URL would have to be done in each of these files. This mechanism takes care of the boring and risky work making the work of the programmer much easier.
#Completion
There are several details, but the basic idea is this. Specific questions can be interesting. See before what has been asked here on the site.
Linked: What are the actions of a controller?
– Jéf Bueno