2016-05-17 4 views
0

Wenn ich auf die Swagger-URL zugreifen: http//localhost:50505/swagger/index. Ich habe den Fehler 500 bekommen.Swagger 500 Fehler in Web-API

Bitte helfen Sie mir herauszufinden.

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

der entsprechende Konfigurationscode in Startup.cs ist

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 der folgenden: der Fehler unable to load swagger ui

+0

Ein Fehler von 500 könnte bedeuten, dass Sie zu viele Dinge hinzufügen könnten, um Ihren Code mit einer höheren Fehlerbehandlung zu versehen [GlobalError Handling] (http://www.asp.net/web-api/overview/error-handling/web-) api-global-error-handling) oder [Ausnahmenfilter] (http://www.asp.net/web-api/overview/error-handling/exception-handling). Alternativ kann ein Tool wie Firebug Ihnen helfen, mehr Details zu erhalten und uns helfen, dieses Problem zu beheben. – JaredStroeb

+0

Ich habe das Bild hinzugefügt. Ähnlich ist es mit [this] (http://stackoverflow.com/questions/35788911/500-error-when-setting-up-swagger-in-asp-net-core-mvc-6-app) und [einem anderen] (http://stackoverflow.com/questions/32973669/web-api-swagger-documentation-error-500) aber ich habe nur keine Ahnung. –

+0

Ich nehme an, Sie verwenden 'ASP.NET Core MVC'? Wenn ja, fügen Sie das Tag der Frage hinzu. – venerik

Antwort

2

Ihr Weg Attribute falsch sind. Die Routen für GetAvailableBandWidth und GetTotalBandWidth sind beide der Route api/bandwidth/{facilityId} zugeordnet und nicht, wie Ihre Kommentare vermuten lassen, api/Bandwidth/GetAvailableBandwidth/{facilityId} und api/Bandwidth/GetTotalBandwidth/{facilityId}. Das gleiche gilt für Ihre Put-Methoden.

Wenn Sie zwei identische Routen registrieren, schlägt einer fehl und löst eine Ausnahme aus. Daraus ergibt sich die HTTP-Statuscode 500

Sie können es wie folgt beheben:

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

Bitte beachten Sie entfernte ich die [FromRoute] Attribute, weil sie nicht notwendig sind.

+0

Ja, es hat das Problem behoben. Es gibt einen anderen Fehler als: InvalidOperationException: 'InvalidOperationException: Mehrere Konstruktoren, die alle angegebenen Argumenttypen akzeptieren, wurden im Typ 'BandwidthRestriction.Controllers.BandwidthController' gefunden. Es sollte nur einen anwendbaren Konstruktor geben. Nicht sicher. –

+0

Der Fehler ist, wenn ich die URL 'http setzen: // localhost: 50505/api/Bandbreite/GetTotalBandwidth/163' –

+0

Das ist, weil Sie zwei Konstruktoren haben. Entferne den zweiten. Sie sollten den 'DbContext' nicht benötigen, wenn Sie bereits ein Repository haben. – venerik