官术网_书友最值得收藏!

Finding controllers

Regardless of whether you go for POCO or non-POCO controllers, ASP.NET Core will apply the same rules for discovering controllers, which are as follows:

  • They need to have the Controller suffix (strictly speaking, this can be changed, but we will leave this for now).
  • They need to be instantiable classes (nonabstract, nongeneric, and nonstatic).
  • They cannot have the [NonController] attribute applied to them.
  • If they are POCO and do not have the Controller suffix, you can decorate them with the [Controller] attribute.
By convention, the files that contain the controller classes are stored in a folder called Controllers, and also in a Controllers namespace, but this is just ignored.

Controller classes are looked up by the name in the route—the controller parameter—and they are searched in the assemblies registered for that purpose. By default, the currently executing assembly is included in the search, but all assemblies registered as application parts are too. You can register additional application parts when you add the MVC features to the dependency injection framework (ConfigureServices method) as follows:

services.AddMvc()
.AddApplicationPart(typeof(MyCustomComponent).GetTypeInfo().Assembly);

Here, we are adding a reference to the assembly that contains a hypothetical class, MyCustomComponent. After we do this, any controllers that are located in it are available for use. In order to get the full list of found controllers, we can use ControllerFeature and populate it through ApplicationPartManager:

services.AddMvc()
.AddApplicationPart(typeof(MyCustomComponent).GetTypeInfo().Assembly)
.ConfigureApplicationPartManager(parts =>
{
var controllerFeature = new ControllerFeature();
parts.PopulateFeature(controllerFeature);
//controllerFeature.Controllers contains the list of discovered
//controllers' types
});

Controllers are only discovered once, at startup time, which is a good thing performance-wise.

If there are two controllers with the same name but that are in different namespaces, and they both expose an action method that matches the current request, then ASP.NET won't know which one to pick and will throw an exception. If this happens, we need to give one of the classes a new controller name by applying a [ControllerName] attribute, as shown in the following code:

namespace Controllers
{
public class HomeController
{
}

namespace Admin
{
[ControllerName("AdminHome")]
public class HomeController
{
}
}
}

We could also change the action name, as we will see in a moment. Now, let's see what happens once the controller type has been found.

主站蜘蛛池模板: 喀什市| 白银市| 滦平县| 陇川县| 澄城县| 长沙县| 建平县| 大名县| 龙川县| 行唐县| 景德镇市| 科尔| 台东市| 长葛市| 伊金霍洛旗| 横山县| 义马市| 江安县| 龙胜| 凌海市| 汶川县| 奇台县| 新平| 临颍县| 若羌县| 类乌齐县| 兴业县| 定结县| 永靖县| 广昌县| 三都| 吉林省| 巴青县| 天峻县| 双鸭山市| 临沂市| 沂水县| 天门市| 云霄县| 汨罗市| 应城市|