How to use a controller in another assembly in ASP.NET Core MVC 2.0?(如何在 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.
Where assembly is the instance Assembly representing Contoso.School.UserService.dll.
您可以从任何包含的类型中获取它或像这样加载它:
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 的另一个程序集中使用控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 ASP.NET Core MVC 2.0 的另一个程序集中使用控制器?
基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
