我有以下设置:这很简单。现在我面临的情况很奇怪。如果我将启动文件放在测试项目中,我会得到所有路由的 404,如果我将该文件移动到WebApplication1,所有路由都会被找到。这是 Startup 类的样子:using Microsoft.AspNetCore.Builder;using Microsoft.AspNetCore.Hosting;using Microsoft.AspNetCore.Mvc;using Microsoft.Extensions.Configuration;using Microsoft.Extensions.DependencyInjection;using Swashbuckle.AspNetCore.Swagger;namespace Test{ public class Startup { public Startup(IConfiguration configuration) { Configuration = configuration; } public IConfiguration Configuration { get; } // This method gets called by the runtime. Use this method to add services to the container. public void ConfigureServices(IServiceCollection services) { services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1); services.AddSwaggerGen(c => { c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" }); }); } // This method gets called by the runtime. Use this method to configure the HTTP request pipeline. public void Configure(IApplicationBuilder app, IHostingEnvironment env) { app.UseSwagger(); // Enable middleware to serve swagger-ui (HTML, JS, CSS, etc.), // specifying the Swagger JSON endpoint. app.UseSwaggerUI(c => { c.SwaggerEndpoint("/swagger/v1/swagger.json", "My API V1"); }); app.UseMvc(); } }}我不能提供更多细节,因为这就是我所拥有的。将班级从一个项目转移到另一个项目会产生问题,我不明白为什么。测试项目只是一个类库:<Project Sdk="Microsoft.NET.Sdk"> <PropertyGroup> <TargetFramework>netcoreapp2.1</TargetFramework> <CopyLocalLockFileAssemblies>true</CopyLocalLockFileAssemblies> </PropertyGroup>问题是,为什么将 Startup 类移动到 Test 项目会阻止路由被初始化?提前致谢。
1 回答
慕神8447489
TA贡献1780条经验 获得超1个赞
我让它工作了,这是修复它的原因:
public virtual void ConfigureServices(IServiceCollection services)
{
var mvcModule = services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
foreach(var assembly in EndpointAssemblies)
{
mvcModule.AddApplicationPart(assembly);
}
其中 EndpointAssemblies 只是已定义程序集的列表。mvcModule.AddControllersAsServices(); }
- 1 回答
- 0 关注
- 239 浏览
添加回答
举报
0/150
提交
取消