I would like to know in more detail how the asp.net core matches the controller's string name with the corresponding class, and how it is created and called. However, it is not possible to find the place where this happens. I would be grateful for the description of this process or at least an indication in which classes to search for the corresponding code.
1 answer
I answer the question as I understood it :)
Requests from the client are routed. And you manage routing as a programmer. Those. you tell the server application what part of the URI to which it corresponds. MVC typically uses special middleware ; self matching in your case will be something like this:
public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory) { // ... app.UseMvc(routes => { routes.MapRoute("add", "add", new { controller = "Product", action = "Add" }); // ... }); // ... } Here it is assumed that you have a controller containing an Add method:
public class ProductController : Controller { // ... public IActionResult Add () { // ... } } Please note that the controller should be called this way: ProductController , and it should be located in the Controllers folder of your project.
There is another approach, it is more often used in WebAPI applications. I will demonstrate with an example:
[Route("[controller]")] public class ProductController : Controller { // ... [Route("add")] public IActionResult Add () { // ... } } That is, you explicitly tell the application how to respond to a URI. The string [Route("[controller]")] points to the name of the controller — in this case, Product . The Add method belongs to the controller class, so the [Route("add")] attribute corresponds to the URI [адрес сервера]/product/add . The rules for naming and location are the same.
It should be noted that any approach can be used in any type of application; from a technical point of view, the “MVC application project” and the “WebAPI application project” are simply sets of presets.
This is an extremely simplified answer that directly answers the question (as I understood it) - and nothing more. The examples omitted a lot of necessary for the correct operation of the ASP.NET application code. Of course, it is necessary to study the materiel, starting with the documentation .
[имя сервера]/product/add? - eastwing