1

When I access the swagger url: http//localhost:50505/swagger/index. I got the 500 error.

Please help me to figure out.

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

The corresponding configuration code in Startup.cs is

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

1

In firefox: the error is unable to load swagger ui

Community
  • 1
  • 1
  • A 500 error could mean a too many things could you add some higher level error handling to your code [GlobalError Handling](http://www.asp.net/web-api/overview/error-handling/web-api-global-error-handling) or [Exception Filters](http://www.asp.net/web-api/overview/error-handling/exception-handling). Alternatively a tool like Firebug may help you get more details and help us debug this problem. – Jared Stroebele May 17 '16 at 15:40
  • I added the image. It is similar with [this](http://stackoverflow.com/questions/35788911/500-error-when-setting-up-swagger-in-asp-net-core-mvc-6-app) and [another](http://stackoverflow.com/questions/32973669/web-api-swagger-documentation-error-500) but I just have no idea. –  May 17 '16 at 15:42
  • I guess you are using `ASP.NET Core MVC`? If so, add that tag to the question. – venerik May 17 '16 at 21:10

1 Answers1

4

Your route attributes are wrong. The routes for GetAvailableBandWidth and GetTotalBandWidth are both mapped to the route api/bandwidth/{facilityId} and not, as your comments suggests, to api/Bandwidth/GetAvailableBandwidth/{facilityId} and api/Bandwidth/GetTotalBandwidth/{facilityId}. The same goes, sort of, for your put methods.

When you register two identical routes, one will fail and throws an exception. Hence the http status code 500.

You can fix it like this:

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

Please note I removed the [FromRoute] attributes because they are not necessary.

venerik
  • 5,766
  • 2
  • 33
  • 43
  • Yes, it fixed the problem. There is another error then: 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.. –  May 17 '16 at 21:20
  • The error is when I put the url `http://localhost:50505/api/Bandwidth/GetTotalBandwidth/163` –  May 17 '16 at 21:37
  • That's because you have two constructors. Remove the second. You shouldn't be needing the `DbContext` when you already have a repository. – venerik May 18 '16 at 04:58