首页 文章

WebApi Swagger:API endpoints 空列表

提问于
浏览
2

Problem: Swagger在我的.NET WebAPI项目中运行正常,但未列出任何API endpoints .

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

                    });
        }
    }
}

这是一个控制器示例:

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注释(因为我的应用程序有一些),但列表中没有弹出任何内容 . 我错过了什么吗?谢谢 .

UPDATE:

  • 第一次尝试:从WebApiConfig.cs调用swagger寄存器 - > NOT WORKING

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

2 回答

  • 1

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

    我想您在WebApiConfig.cs中缺少以下行

    public static void Register(HttpConfiguration config)
    {
       ...
       config.MapHttpAttributeRoutes();
       ...
    }
    

    它将注册您的web api的所有路由 .

相关问题