gpt4 book ai didi

c# - WebApi Swagger : API Endpoints empty list

转载 作者:行者123 更新时间:2023-11-30 16:40:52 25 4
gpt4 key购买 nike

问题:Swagger 在我的 .NET WebAPI 项目中运行良好,但未列出任何 API 端点。

enter image description here

这是我从中得到的:http://localhost:62536/swagger/docs/v1

{
"swagger":"2.0",
"info":{
"version":"v1",
"title":"Backend.WebApi"
},
"host":"localhost:62536",
"schemes":[
"http"
],
"paths":{

},
"definitions":{

}
}

swaggerConfig.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 =>

});
}
}
}

这是一个 Controller 示例:

testController.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();
}
[...]

我也尝试启用 XML 注释(因为我的应用程序有一些注释),但列表中没有弹出任何内容。我错过了什么吗?谢谢。

更新:

  • 第一次尝试:从 WebApiConfig.cs 调用 swagger 注册 -> 不工作

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();
}

swaggerConfig.cs

// [assembly: PreApplicationStartMethod(typeof(SwaggerConfig), "Register")]

namespace Backend.WebApi

最佳答案

将配置部分从 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();

关于c# - WebApi Swagger : API Endpoints empty list,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50412581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com