I think you want to know about routing as a whole. It is the way to direct incoming HTTP requests to the appropriate controller methods. So as the information comes in this request a different method will be called.
The route is the path it will take to execute something, depending on the context, somehow 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, looks for a pattern, and uses a criterion to decide what to do. Details about this can be configured programmatically in addition to the standard that it adopts by convention according to the strings .
In the older versions it used the same classic ASP.Net routing system. In the .Net Core there is a system of its own.
Example
An example route created in class RouteConfig
:
routes.MapRoute("Default", //nome da rota
"{controller}/{action}/{id}", // padrão do URL
new { controller = "Home", action = "Index", id = "" } // parâmetros
Then what comes first at the beginning of the parameters in the URL (under normal conditions right after the domain and perhaps the port) is what will determine which controller class will be called.
What comes next as if it were a subdirectory is the action, that is, the method of this class that should be executed. But it may be that other parts are required 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, find what will be passed to the method as argument. Your type could be considered in the signature.
The object with default values is also set if there is nothing useful to establish the route.
The class that will process this would look something like this:
public class HomeController : Controller {
public ActionResult Index(string id) {
return View();
}
}
You could have a note indicating the specific verb that is accepted.
I could call it like this:
dominio.com/Home/Index/101
Attributes
You can also define routes with attributes . Example:
[Route(“{produtoId:int}”)]
public ActionResult Edita(int produtoId) {
return View();
}
Alternative to routing
All routes form a mapping between what comes from outside and the code. Without the routes would have to have a file for each action and the interpretation of the arguments contained in the URL would have to be made in each of these files. This engine takes care of boring and risky work, making it much easier for the programmer to work.
Conclusion
It has several details, but by the question the basic idea is this. Specific questions can be interesting. Check out what has already been asked here on the site.