c# - WebApi Swagger : API Endpoints empty list

标签 c# asp.net-web-api swagger owin swashbuckle

问题: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/

相关文章:

java - 如何更改 Swagger-ui URL 前缀?

Java - 如何直接从 openapi 3.0 规范生成 Swagger UI

c# - 从 asp.net 页面运行 cmd 或 exe 文件

c# - LINQ where db table does not contain in memory list elements

c# - IF 语句的内联赋值

c# - 在 ApiController 上执行 POST 时处理发送的 id 的最佳实践?

c# - 如何使用 CSV 助手附加 CSV?

asp.net-mvc - 具有可为空 Guid 的 Web Api GET 方法可能吗?

c# - 如何使用 Unity 容器解决 Web API 中依赖项的依赖关系?

python - Django 休息框架 : How to enable swagger docs for function based views