2

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

3
  • A 500 error could mean a too many things could you add some higher level error handling to your code GlobalError Handling or Exception Filters. Alternatively a tool like Firebug may help you get more details and help us debug this problem. Commented May 17, 2016 at 15:40
  • I added the image. It is similar with this and another but I just have no idea. Commented May 17, 2016 at 15:42
  • I guess you are using ASP.NET Core MVC? If so, add that tag to the question. Commented May 17, 2016 at 21:10

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

3 Comments

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..
The error is when I put the url http://localhost:50505/api/Bandwidth/GetTotalBandwidth/163
That's because you have two constructors. Remove the second. You shouldn't be needing the DbContext when you already have a repository.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.