Dyd*_*666 5 c# asp.net-web-api swagger owin swashbuckle
问题: Swagger 在我的 .NET WebAPI 项目中运行良好,但没有列出 API 端点。
这是我从:http://localhost:62536/swagger/docs/v1
{
"swagger":"2.0",
"info":{
"version":"v1",
"title":"Backend.WebApi"
},
"host":"localhost:62536",
"schemes":[
"http"
],
"paths":{
},
"definitions":{
}
}
Run Code Online (Sandbox Code Playgroud)
招摇配置.cs
using System.Web.Http;
using WebActivatorEx;
using Backend.WebApi;
using Swashbuckle.Application;
[assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace Backend.WebApi
{
public class SwaggerConfig
{
public static void Register()
{
var thisAssembly = typeof(SwaggerConfig).Assembly;
GlobalConfiguration.Configuration
.EnableSwagger(c =>
{
c.SingleApiVersion("v1", "Backend.WebApi");
})
.EnableSwaggerUi(c =>
});
}
}
}
Run Code Online (Sandbox Code Playgroud)
这是一个控制器示例:
测试控制器.cs
namespace Backend.WebApi.Controllers
{
[Authorize]
[RoutePrefix("api/test")]
public class TestController : ApiController
{
private readonly IAppService _appService;
public TestController(IAppService appService)
{
_appService = appService;
}
[Route("vehicles/all")]
public List<VehicleViewModel> GetVehicles()
{
return _appService.GetVehicles();
}
[...]
Run Code Online (Sandbox Code Playgroud)
我也尝试启用 XML 注释(因为我的应用程序有一些),但列表中没有弹出任何内容。我错过了什么吗?谢谢。
更新:
webConfigApi.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
AutoMapperConfiguration.Configure();
SwaggerConfig.Register();
ConfigureIoC(config);
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
var jsonFormatter = config.Formatters.OfType<JsonMediaTypeFormatter>().First();
jsonFormatter.SerializerSettings.ContractResolver = new CamelCasePropertyNamesContractResolver();
}
Run Code Online (Sandbox Code Playgroud)
招摇配置.cs
// [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace Backend.WebApi
Run Code Online (Sandbox Code Playgroud)
将配置部分从 移动SwaggerConfig.cs
到WebApiConfig.cs
修复问题,可能是由于我们OWIN
在我们的项目中使用的事实。
webApiConfig.cs
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
[...]
config
.EnableSwagger(c => c.SingleApiVersion("v1", "Backend.WebApi"))
.EnableSwaggerUi();
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1836 次 |
最近记录: |