【问题标题】:.NET Core and Swagger API generation.NET Core 和 Swagger API 生成
【发布时间】:2017-11-10 10:49:43
【问题描述】:

我正在创建一个准系统 .NET Core Web api 项目(从下面的空白模板开始) https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-net-core/

下面的代码运行良好,直到我添加了 Swashbuckle.AspNetCore(以及下面的配置代码),现在我们得到了这个错误

InvalidOperationException:未注册“Microsoft.AspNetCore.Mvc.ApiExplorer.IApiDescriptionGroupCollectionProvider”类型的服务。

有什么想法吗?

请注意:我们没有使用“builder.AppMvc()”,因为我们试图尽可能地精简这个 api。

public class Startup
{
    // This method gets called by the runtime. Use this method to add services to the container.
    // For more information on how to configure your application, visit https://go.microsoft.com/fwlink/?LinkID=398940
    public void ConfigureServices(IServiceCollection services)
    {
        var builder = services.AddMvcCore();

        builder.AddAuthorization();
        builder.AddFormatterMappings();
        builder.AddJsonFormatters();
        builder.AddCors();

        // Register the Swagger generator, defining one or more Swagger documents
        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, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole();

        if (env.IsDevelopment())
            app.UseDeveloperExceptionPage();



        app.UseMvc();


        // Enable middleware to serve generated Swagger as a JSON endpoint.
        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");
        });
    }
}

[Route("api/[controller]")]
public class ValuesController : ControllerBase
{
    // GET api/values
    [HttpGet]
    public IActionResult Get()
    {
        return Ok(new[] {"value1", "value2"});
    }
}

【问题讨论】:

    标签: c# swagger


    【解决方案1】:

    解决方法:在 Startup.cs 中使用 AddMvc() 代替 AddMvcCore() 就可以了。

    或写:

    services.AddMvcCore().AddApiExplorer();
    

    这些链接可以提供帮助:

    No service for type 'Microsoft.AspNetCore.Mvc.ViewFeatures.ITempDataDictionaryFactory' has been registered

    https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/299

    【讨论】:

    • 我假设您在我的代码中引用了这一行? var builder = services.AddMvcCore();"
    • 我不能使用 AddMvc 作为这个错误。这个项目是从一个空白的 webapi 代码项目开始的。
    • 然后试试 services.AddMvcCore() .AddApiExplorer();
    • 是的..那行得通..我正在输入与您相同的响应。你最初的回答给了我我需要的所有方向。太棒了,谢谢你。现在,为 IoC 添加结构图,看看大摇大摆地打破了什么。我过去在常规 MVC 项目中遇到过这个问题
    【解决方案2】:

    对于 ASP.NET Core 2.2,您应该编写:

    services.AddMvcCore().AddApiExplorer();
    

    【讨论】:

      【解决方案3】:

      如果您尝试将带有 Swagger UI 的 AspNetCore MVC 控制器添加到 AspNetCore Web 应用程序中,也会出现这些类型的错误。只需将它们分成两个不同的项目:mvc 控制器和 Web 应用程序。

      【讨论】: