问题:
在我的生活中,昂首阔步很好。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":{
}
}
大摇大摆。反恐精英
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 =>
});
}
}
}
以下是一个控制器示例:
测试控制器。反恐精英
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();
}
[...]
我也尝试过启用XML注释(因为我的应用程序中有一些注释),但列表中没有弹出任何内容。
我错过什么了吗?
谢谢
更新:
-
第一次尝试
:从WebApiConfig调用swagger寄存器。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();
}
大摇大摆。反恐精英
// [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]
namespace Backend.WebApi