【问题标题】:Swagger 500 error in web apiWeb api中的Swagger 500错误
【发布时间】:2016-05-17 15:22:46
【问题描述】:

当我访问招摇网址时: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

【问题讨论】:

  • 500 错误可能意味着太多事情,您可以在代码GlobalError HandlingException Filters 中添加一些更高级别的错误处理。或者,像 Firebug 这样的工具可以帮助您获取更多详细信息并帮助我们调试此问题。
  • 我添加了图片。 thisanother 类似,但我不知道。
  • 我猜你正在使用ASP.NET Core MVC?如果是这样,请将该标签添加到问题中。

标签: asp.net asp.net-web-api asp.net-core swagger swashbuckle


【解决方案1】:

您的路线属性错误。 GetAvailableBandWidthGetTotalBandWidth 的路由都映射到路由 api/bandwidth/{facilityId},而不是像您的 cmets 建议的那样映射到 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] 属性,因为它们不是必需的。

【讨论】:

  • 是的,它解决了问题。然后还有另一个错误: InvalidOperationException: InvalidOperationException: Multiple constructors accepting all given argument types have been found in type 'BandwidthRestriction.Controllers.BandwidthController'. There should only be one applicable constructor. Not sure..
  • 错误是我把网址http://localhost:50505/api/Bandwidth/GetTotalBandwidth/163
  • 那是因为你有两个构造函数。删除第二个。当您已经有一个存储库时,您不应该需要 DbContext
猜你喜欢
  • 2016-01-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多