路由未初始化

我有以下设置:

http://img1.mukewang.com/61a338fb00010b0204670747.jpg

这很简单。现在我面临的情况很奇怪。如果我将启动文件放在测试项目中,我会得到所有路由的 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 项目会阻止路由被初始化?


提前致谢。


弑天下
浏览 248回答 1
1回答

慕神8447489

我让它工作了,这是修复它的原因:public virtual void ConfigureServices(IServiceCollection services){&nbsp; &nbsp; var mvcModule = services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);&nbsp; &nbsp; foreach(var assembly in EndpointAssemblies)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; mvcModule.AddApplicationPart(assembly);&nbsp; &nbsp; }其中 EndpointAssemblies 只是已定义程序集的列表。mvcModule.AddControllersAsServices(); }
打开App,查看更多内容
随时随地看视频慕课网APP