2017-06-08 75 views
0

我创建一个准系统.NET核心Web API项目 https://andrewlock.net/removing-the-mvc-razor-dependencies-from-the-web-api-template-in-asp-net-core/.NET的核心和扬鞭API代

下面的代码是工作的罚款(从下面空白模板开始),直到我加入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"}); 
    } 
} 

回答

2

解决方案:使用AddMvc()而不是AddMvcCore()在Startup.cs,它会工作。

或写:

services.AddMvcCore().AddApiExplorer(); 

这些链接可以帮助:

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

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

+0

我假设你指的这条线在我的代码? var builder = services.AddMvcCore();“ – JCircio

+0

我不能使用AddMvc作为这个错误,这个项目是从一个空白的webapi代码项目开始的 – JCircio

+0

然后尝试services.AddMvcCore() .AddApiExplorer(); –