在一个解决方案中,我拥有asp.net核心MVC项目和单独的WebApi项目。我在github上的文档后添加了招摇。这是我的mvc项目的Startup.cs:
public void ConfigureServices(IServiceCollection services)
{
//...
// Adding controllers from WebApi:
var controllerAssembly = Assembly.Load(new AssemblyName("WebApi"));
services.AddMvc(o =>
{
o.Filters.Add<GlobalExceptionFilter>();
o.Filters.Add<GlobalLoggingFilter>();
})
.AddApplicationPart(controllerAssembly)
.AddJsonOptions(options =>
{
options.SerializerSettings.ReferenceLoopHandling = ReferenceLoopHandling.Ignore;
});
services.AddSwaggerGen(c =>
{
//The generated Swagger JSON file will have these properties.
c.SwaggerDoc("v1", new Info
{
Title = "Swagger XML Api Demo",
Version = "v1",
});
});
//...
}
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
//...
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Swagger XML Api Demo v1");
});
//...
app.UseMvc(routes =>
{
// ...
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}");
});
}
这里是小众:
WebApi控制器的属性路由:
[Route("api/[controller]")]
public class CategoriesController : Controller
{
// ...
[HttpGet]
public async Task<IActionResult> Get()
{
return Ok(await _repo.GetCategoriesEagerAsync());
}
// ...
}
当我尝试转到/ swagger时,找不到/swagger/v1/swagger.json:
我做错了什么?
谢谢!
答案 0 :(得分:2)
这个问题困扰了我好几个小时...我找到了原因...
检查下面的代码!
.. Startup.cs ..
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseHsts();
}
app.UseHttpsRedirection();
app.UseMvc();
app.UseSwagger(); // if I remove this line, do not work !
app.UseSwaggerUi3();
}
答案 1 :(得分:0)
也想在这里补充我的经验。
我在configureServices
中将版本指定为V1
(注意,大写字母为V
)和
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_2);
services.AddSwaggerGen(c =>
{
c.SwaggerDoc("v1", //this v was in caps earlier
new Swashbuckle.AspNetCore.Swagger.Info
{
Version = "v1",//this v was in caps earlier
Title = "Tiny Blog API",
Description = "A simple and easy blog which anyone love to blog."
});
});
//Other statements
}
然后在configure
方法中是小写
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
app.UseMvc();
app.UseSwagger();
app.UseSwaggerUI(c =>
{
c.SwaggerEndpoint("/swagger/v1/swagger.json", "Tiny Blog V1");
});
}
也许可以帮助某人。