代码之家  ›  专栏  ›  技术社区  ›  MeTitus

路由未初始化

  •  1
  • MeTitus  · 技术社区  · 6 年前

    Solution structure

    这很简单。现在我面临的情况很奇怪。如果我把启动文件放在 测试 如果我把文件移到 Web应用程序1

    这就是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>
    
      <ItemGroup>
        <PackageReference Include="Microsoft.AspNetCore" Version="2.1.1" />
        <PackageReference Include="Microsoft.AspNetCore.App" Version="2.1.1" />
        <PackageReference Include="Swashbuckle.AspNetCore.Swagger" Version="3.0.0" />
        <PackageReference Include="Swashbuckle.AspNetCore.SwaggerGen" Version="3.0.0" />
        <PackageReference Include="Swashbuckle.AspNetCore.SwaggerUI" Version="3.0.0" />
      </ItemGroup>
    
    </Project>
    

    更新

    提前谢谢。

    更新

    测试项目实际上只是web项目的一组公共基类,很抱歉名称不对。因此,如果我将Startup类从Common.Web项目移到WebApplication1并更新名称空间,那么一切正常,否则就会停止工作。调试显示正在调用的启动类,这更奇怪。

    Solution structure

    1 回复  |  直到 6 年前
        1
  •  2
  •   MeTitus    6 年前

    我把它修好了,这就是修好它的原因:

    public virtual void ConfigureServices(IServiceCollection services)
    {
        var mvcModule = services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);
    
        foreach(var assembly in EndpointAssemblies)
        {
            mvcModule.AddApplicationPart(assembly);
        }
    

    其中EndpointAssemblies只是已定义程序集的列表。 }