如何在ASP.NET Core MVC 2.0中的另一个程序集中使用控制器? [英] How to use a controller in another assembly in ASP.NET Core MVC 2.0?

查看:124
本文介绍了如何在ASP.NET Core MVC 2.0中的另一个程序集中使用控制器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为了模块化,我在不同的程序集中创建了一些控制器.每个程序集代表整个系统的有限上下文(模块,子系统,部门等).

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 the overall system.

每个模块的控制器都是由对其他模块一无所知的人开发的,并且一个中央协调器将在单个应用程序中涵盖所有这些模块.

Each module's controllers are developed by someone that knows nothing about other modules, and a central orchestrator is about to cover all these modules in one single application.

因此,有一个名为school的模块,其中有一个TeacherController.它的输出是Contoso.School.UserService.dll.

So, there is this module called school, and it has a TeacherController in it. The output of it is Contoso.School.UserService.dll.

主要的协调器称为Education,它引用了Contoso.School.UserService.dll.

The main orchestrator is called Education and it has a reference to Contoso.School.UserService.dll.

我的program.cs是:

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args).UseKestrel()
            .UseStartup<Startup>()
            .Build();

对于教师控制器的路由,我得到了404.如何在其他程序集中使用控制器?

Yet for the routes of teacher controller, I get 404. How to use controllers in other assemblies?

推荐答案

Startup类的ConfigureServices方法内部,您必须调用以下内容:

Inside the ConfigureServices method of the Startup class you have to call the following:

services.AddMvc().AddApplicationPart(assembly).AddControllersAsServices();

其中assembly是表示Contoso.School.UserService.dll的实例Assembly.

您可以加载它,也可以从任何包含的类型获取它,也可以这样加载:

You can load it either getting it from any included type or like this:

var assembly = Assembly.Load("Contoso.School.UserService");

这篇关于如何在ASP.NET Core MVC 2.0中的另一个程序集中使用控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆