简体   繁体   English

使用 .NET Core 3.1 中的 TestServer 加载额外的 controller Web ZDB974238714CA8DE634A7CE1D0Z

[英]Load additional controller using TestServer in .NET Core 3.1 Web API

I'm using the following code in my unit tests:我在单元测试中使用以下代码:

var hostBuilder = new HostBuilder()
        .ConfigureWebHost(webHost =>
        {
            // Add TestServer
            webHost.UseTestServer();
            webHost.UseStartup<Startup>();

        });

I'd like to add a new controller only in the testserver.我只想在测试服务器中添加一个新的 controller。 Is this somehow possible using the same StartUp in my normal und unit test project?在我的正常 und 单元测试项目中使用相同的 StartUp 是否有可能?

Update: To make it clear:更新:为了清楚起见:

I have my main controllers in MainProject.Controllers.我在 MainProject.Controllers 中有我的主要控制器。 When creating my TestServer in my UnitTestProject I want to additionally load UnitTestProject.Controllers (but only for the TestServer).在我的 UnitTestProject 中创建我的 TestServer 时,我想另外加载 UnitTestProject.Controllers(但仅适用于 TestServer)。

If you want to add new controller, you can just create new file under ProjectName.Controller namespace.如果要添加新的 controller,只需在ProjectName.Controller命名空间下创建新文件即可。

I think I found the answer:我想我找到了答案:

I've derived a TestStartup (in my UnitTestProject) class from Startup.我从 Startup 派生了一个 TestStartup(在我的 UnitTestProject 中)class。 This seems to do the trick.这似乎可以解决问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 .NET Core 3.1 Web API 单元测试中运行 TestServer 时扩展/自定义 IApplicationBuilder - Extend/Custom IApplicationBuilder when running TestServer in a .NET Core 3.1 Web API unit test .Net Core 3.1 Web API 在新控制器上抛出 404 - .Net Core 3.1 Web API Throwing 404 on new Controller 如何在 .Net Core 3.1 Web API 控制器中获取声明? - How to get claims in .Net Core 3.1 Web API Controller? ASP.Net Core 3.1 WEB API - Controller 方法不返回正确的 Z0ECD11C1D7A287401F814A8 - ASP.Net Core 3.1 WEB API - Controller method doesn't return correct JSON 无法访问 ASP.NET Core 3.1 Web API Z9BBF373797BF7CF7BA6ZC8002 中的端点,5 - Unable to access an endpoint in ASP.NET Core 3.1 Web API Controller, from View ASP.NET Core 3.1 Web API with $.ajax--加载资源失败400错误 - ASP.NET Core 3.1 Web API with $.ajax--failed to load resource 400 error 使用Autofac在.NET Core 2.0 Web API控制器上进行拦截 - Interception on .NET Core 2.0 Web API Controller By Using Autofac 在web api控制器中使用async / await或task(.net core) - Using async/await or task in web api controller (.net core) 如何在 api mvc 项目 net core 3.1 中向 controller 添加操作 - How to add action to controller in api mvc project net core 3.1 .net 核心 3.1 中 [RoutePrefix("api/{controller})] 的替代方案是什么? - What is alternate for [RoutePrefix("api/{controller})] in .net core 3.1?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM