在asp.netcore 2 web. api项目中,我使用以下教程添加了Swagger:https://learn.microsoft.com/en-us/aspnet/core/tutorials/getting-started-with-swashbuckle?tabs=visual-studio,Visual Studio xml
而且很完美
然后我不得不重新运行函数以从实体框架db-first构建模型,在我在swagger gui中遇到这个错误之后:
No operations defined in spec!
我已经删除了一个读出的大摇大摆,但没有改变。下载
/swagger/v1/swagger.json
它看起来像这样:
{"swagger":"2.0","info":{"version":"v1","title":"My API"},"paths":{},"definitions":{}}
启动. cs:
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.PlatformAbstractions;
using SmoEventApi.Models;
using Swashbuckle.AspNetCore.Swagger;
#region AddedUsings
using Microsoft.EntityFrameworkCore;
#endregion
namespace SmoEventApi
{
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
#region ConfigureServices
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
// Register the Swagger generator, defining one or more Swagger documents
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", new Info { Title = "My API", Version = "v1" });
});
var connection = @"Server=xxxx\MSSQL01;Database=yyy;User Id=user;Password=pp;ConnectRetryCount=0";
services.AddDbContext<DCMMContext>(options => options.UseSqlServer(connection));
}
#endregion
// 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(Configuration.GetSection("Logging"));
loggerFactory.AddDebug();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
app.UseBrowserLink();
}
else
{
app.UseExceptionHandler("/Home/Error");
}
app.UseStaticFiles();
// 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");
});
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
}
}
我不知道为什么我不再从斯瓦格那里得到任何信息了?
Kåre
原来是缺少的控制器中的注释