问题描述
由于某种原因,当在单独的程序集中创建ASP.NET核心控制器时,客户端发出请求时,TestServer无法找到控制器动作.(导致404响应)这是为什么?我该如何解决?这里是重现步骤.
For some reason, when ASP.NET core controllers are created in separate assembly, the TestServer is not able to find controller actions when the client makes a request.(results in 404 response) Why is this? How can I work around it?Here are steps to reproduce.
- 使用.NET core创建新的ASP.NET Core WebAPI
- 在单独的项目中创建集成测试,并将测试配置为使用TestServer()客户端,并使测试成功运行.
- 现在,将控制器分离到其自己的共享库中,并重构在步骤1中创建的项目,以改用此共享库.
- 重新运行包含TestServer()类的测试.您现在会注意到它失败了.
请参阅下面的链接以创建集成测试. 带有ASP.NET Core的集成测试
See the follwing link for creating the integration tests. Integration testing w/ ASP.NET Core
推荐答案
实际上我现在已经找到了解决方案,请参见下面的差异:
Actually I found a solution for now, see diff below:
听起来这可能是TestServer()类的错误以及它在测试运行期间如何托管应用程序.
It sounds like this may be bug of TestServer() class and how it is hosting the application during the test run.
这是代码行,以防您在图像上方无法阅读
Here is the line of code in case you cannot read above in image
.AddApplicationPart(Assembly.Load(new AssemblyName("WebApiToReproduceBug.Controllers")));
这篇关于当控制器与asp.net核心应用程序位于单独的组件中时,为什么TestServer无法找到控制器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!