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.

  • What do you mean by the string name of the controller? And who should create and call the controller? Let's take an example: what to do? - eastwing
  • I want to understand what is the most reasonable way to call the Add () method of the Product class when getting the string "Product / Add" from the user. I thought that the most obvious implementation of this can be found in asp.net. - Konstantin Galiakhmetov
  • @KonstantinGaliahmetov the creation of the controller takes place via the IControllerFactory / IControllerActivator - see the implementation in the framework source code . - PashaPash
  • @ KonstantinGaliahmetov, that is, you want to understand how to properly handle the request from the client [имя сервера]/product/add ? - eastwing
  • @eastwing I want to understand how the creation of the Product class is done by its name. That is, the user receives the class name as a string and I want to match this name with the existing class and call it. I want to see how this works in asp.net. - Konstantin Galiakhmetov

1 answer 1

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 .