How to use a controller in another assembly in ASP.NET Core MVC 2.0?

前端 未结 5 1818
一整个雨季
一整个雨季 2020-12-01 06:16

For the sake of modularity, I have created some controllers in different assemblies. Each assembly represents a bounded context (a module, a sub-system, a division, etc.) of

5条回答
  •  春和景丽
    2020-12-01 06:44

    There is nothing wrong with the above answers, I just use a 1 liners as I know the class that is included in the external assembly that I'd like to load.

    The bellow sample comes from the ASP-WAF firewall and is used to load reporting endpoints and dashboard web pages in an existing web application in .net Core 3.1.

    services.AddMvc(options =>
    {                
        options.Filters.Add();
        options.Filters.Add();
    }).AddApplicationPart(Assembly.GetAssembly(typeof(Walter.Web.FireWall.DefaultEndpoints.ReportingController)));
    

    so to answer your question, let's assume that the TeacherController is in namespace Contoso.School.UserService your implementation would be:

    services.AddMvc(options =>
    {                
       //any option    }).AddApplicationPart(Assembly.GetAssembly(typeof(Contoso.School.UserService.TeacherController )));
    

    or, if you do not have any options then just ignore them and use this:

    services.AddMvc()  .AddApplicationPart(Assembly.GetAssembly(typeof(Contoso.School.UserService.TeacherController)));
    

    If you are not sure about the class in the assembly then us intelisence in your code starting with the namespace and look for a type to use or open object browser in visual studio

    Let me know if you have any issues.

提交回复
热议问题