Web 中的 Swagger 500 错误 api

Swagger 500 error in web api

当我访问 swagger url 时:http//localhost:50505/swagger/index。我收到 500 错误。

请大家帮我想想办法

namespace BandwidthRestriction.Controllers
{
[Route("api/[controller]")]
public class BandwidthController : Controller
{
    private SettingDbContext _context;
    private readonly ISettingRespository _settingRespository;

    public BandwidthController(ISettingRespository settingRespository)
    {
        _settingRespository = settingRespository;
    }
    public BandwidthController(SettingDbContext context)
    {
        _context = context;
    }

    // GET: api/Bandwidth
    [HttpGet]
    public IEnumerable<Setting> GetSettings()
    {
        return _settingRespository.GetAllSettings();
    }

    // GET: api/Bandwidth/GetTotalBandwidth/163
    [HttpGet("{facilityId}", Name = "GetTotalBandwidth")]
    public IActionResult GetTotalBandwidth([FromRoute] int facilityId)
    {
        // ...
        return Ok(setting.TotalBandwidth);
    }

    // GET: api/Bandwidth/GetAvailableBandwidth/163
    [HttpGet("{facilityId}", Name = "GetAvailableBandwidth")]
    public IActionResult GetAvailableBandwidth([FromRoute] int facilityId)
    {
        // ...
        var availableBandwidth = setting.TotalBandwidth - setting.BandwidthUsage;
        return Ok(availableBandwidth);
    }

    // PUT: api/Bandwidth/UpdateBandwidthChangeHangup/163/10
    [HttpPut]
    public void UpdateBandwidthChangeHangup([FromRoute] int facilityId, [FromRoute]int bandwidth)
    {
        _settingRespository.UpdateBandwidthHangup(facilityId, bandwidth);
    }

    // PUT: api/Bandwidth/UpdateBandwidthChangeOffhook/163/10
    [HttpPut]
    public void UpdateBandwidthChangeOffhook([FromRoute] int facilityId, [FromRoute] int bandwidth)
    {
        _settingRespository.UpdateBandwidthOffhook(facilityId, bandwidth);
    }

    // POST: api/Bandwidth/PostSetting/163/20
    [HttpPost]
    public bool PostSetting([FromRoute] int facilityId, [FromRoute]int bandwidth)
    {
        //
        return false;
    }
}

Startup.cs中对应的配置代码为

public void ConfigureServices(IServiceCollection services)
    {
        // Add framework services.
        services.AddEntityFramework()
            .AddSqlServer()
            .AddDbContext<SettingDbContext>(options =>
                options.UseSqlServer(Configuration["Data:DefaultConnection:ConnectionString"]));

        services.AddMvc();
        services.AddSwaggerGen();
        services.ConfigureSwaggerDocument(options =>
        {
            options.SingleApiVersion(new Info
            {
                Version = "v1",
                Title = "Bandwidth Restriction",
                Description = "Api for Bandwidth Restriction",
                TermsOfService = "None"
            });
            // options.OperationFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlActionComments(pathToDoc));
        });

        services.ConfigureSwaggerSchema(options =>
        {
            options.DescribeAllEnumsAsStrings = true;
            //options.ModelFilter(new Swashbuckle.SwaggerGen.XmlComments.ApplyXmlTypeComments(pathToDoc));
        });
        // Add application services.
        services.AddTransient<ISettingRespository, SettingRespository>();
    }

    // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
    public void Configure(IApplicationBuilder app, IHostingEnvironment env, ILoggerFactory loggerFactory)
    {
        loggerFactory.AddConsole(Configuration.GetSection("Logging"));
        loggerFactory.AddDebug();

        app.UseIISPlatformHandler(options => options.AuthenticationDescriptions.Clear());
        app.UseStaticFiles();
        app.UseMvc(routes =>
        {
            routes.MapRoute(
                name: "default",
                template: "{controller}/{action}/{facilityId?}");
            routes.MapRoute(
                name: "",
                template: "{controller}/{action}/{facilityId}/{bandwidth}");
        });

        app.UseSwaggerGen();
        app.UseSwaggerUi();
    }

在 firefox 中:错误是 unable to load swagger ui

你的路由属性有误。 GetAvailableBandWidthGetTotalBandWidth 的路由都映射到路由 api/bandwidth/{facilityId},而不是如您的评论所暗示的那样映射到 api/Bandwidth/GetAvailableBandwidth/{facilityId}api/Bandwidth/GetTotalBandwidth/{facilityId}。您的 put 方法在某种程度上也是如此。

当您注册两个相同的路由时,一个会失败并抛出异常。因此 http 状态代码 500.

你可以这样修复:

// GET: api/Bandwidth/GetTotalBandwidth/163
[HttpGet("GetTotalBandwidth/{facilityId}", Name = "GetTotalBandwidth")]
public IActionResult GetTotalBandwidth(int facilityId)
{
    // ...
    return Ok(setting.TotalBandwidth);
}

// GET: api/Bandwidth/GetAvailableBandwidth/163
[HttpGet("GetAvailableBandwidth/{facilityId}", Name = "GetAvailableBandwidth")]
public IActionResult GetAvailableBandwidth(int facilityId)
{
    // ...
    var availableBandwidth = setting.TotalBandwidth - setting.BandwidthUsage;
    return Ok(availableBandwidth);
}

// PUT: api/Bandwidth/UpdateBandwidthChangeHangup/163/10
[HttpPut("UpdateBandwidthChangeHangup/{facilityId}/{bandwidth}")]
public void UpdateBandwidthChangeHangup(int facilityId, int bandwidth)
{
    _settingRespository.UpdateBandwidthHangup(facilityId, bandwidth);
}

// PUT: api/Bandwidth/UpdateBandwidthChangeOffhook/163/10
[HttpPut("UpdateBandwidthChangeOffhook/{facilityId}/{bandwidth}")]
public void UpdateBandwidthChangeOffhook(int facilityId, int bandwidth)
{
    _settingRespository.UpdateBandwidthOffhook(facilityId, bandwidth);
}

请注意,我删除了 [FromRoute] 属性,因为它们不是必需的。