2016-12-01 96 views
0

我創建了一個新的ASP.NET核心Web API,並有多個控制器比如這個:ASP.NET核心Web API拋出HTTP 500

[Route("api/[controller]")] 
public class DoctorRevenueController : Controller 
{ 
    private IDoctorRevenueRepository DoctorRevenueRepository; 
    public DoctorRevenueController(IDoctorRevenueRepository repository) 
    { 
     DoctorRevenueRepository = repository; 
    } 
    protected override void Dispose(bool disposing) 
    { 
     base.Dispose(disposing); 
    } 

    [HttpGet("GetDoctorRevenues")] 
    //[Route("DoctorRevenue")] 
    public async Task<IActionResult> GetDoctorRevenues(Int32? pageSize = 10, Int32? pageNumber = 1, String DoctorName = null) 
    { 
     var response = new ListModelResponse<DocRevViewModel>() as IListModelResponse<DocRevViewModel>; 

     try 
     { 
      response.PageSize = (Int32)pageSize; 
      response.PageNumber = (Int32)pageNumber; 

      response.Model = await Task.Run(() => 
      { 
       return DoctorRevenueRepository 
       .GetDocRevenues(response.PageNumber, response.PageSize, DoctorName) 
       .Select(item => item.ToViewModel()) 
       .ToList(); 
      }); 
      response.Message = String.Format("Total Records {0}", response.Model.Count()); 
     } 
     catch (Exception ex) 
     { 
      response.DidError = true; 
      response.Message = ex.Message; 
     } 
     return response.ToHttpResponse(); 
    } 

    //GET DoctorRevenues/Doctor 
    [HttpGet("GetDoctorRevenue/{DoctorId}")] 
    //[Route("DoctorRevenue")] 
    public async Task<IActionResult> GetDoctorRevenue(int DoctorId) 
    { 
     var response = new SingleModelResponse<DocRevViewModel>() as ISingleModelResponse<DocRevViewModel>; 

     try 
     { 
      response.Model = await Task.Run(() => 
      { 
       return DoctorRevenueRepository.GetDocRevenue(DoctorId).ToViewModel(); 
      }); 
     } 
     catch (Exception ex) 
     { 
      response.DidError = true; 
      response.Message = ex.Message; 
     } 
     return response.ToHttpResponse(); 
    } 

    //POST DoctorRevenues/Doctor 
    [HttpPost("CreateDoctorRevenue/{DoctorId}")] 
    //[Route("DoctorRevenue")] 
    public async Task<IActionResult> CreateDoctorRevenue([FromBody]DocRevViewModel value) 
    { 
     var response = new SingleModelResponse<DocRevViewModel>() as ISingleModelResponse<DocRevViewModel>; 

     try 
     { 
      var entity = await Task.Run(() => 
      { 
       return DoctorRevenueRepository.AddDocRevenue(value.ToEntity()); 
      }); 
      response.Model = entity.ToViewModel(); 
      response.Message = "The invoices and revenue for this doctor have been successfully saved."; 
     } 
     catch(Exception ex) 
     { 
      response.DidError = true; 
      response.Message = ex.Message; 
     } 
     return response.ToHttpResponse(); 
    } 

    //PUT DoctorRevenues/Doctor/5 
    [HttpPut("UpdateDoctorRevenue/{RecordId}")] 
    //[Route("DoctorRevenue/{RecordId}")] 
    public async Task<IActionResult> UpdateDoctorRevenue(int RecordId, [FromBody]DocRevViewModel value) 
    { 
     var response = new SingleModelResponse<DocRevViewModel>() as ISingleModelResponse<DocRevViewModel>; 

     try 
     { 
      var entity = await Task.Run(() => 
      { 
       return DoctorRevenueRepository.UpdateDocRevenue(RecordId, value.ToEntity()); 
      }); 
      response.Model = entity.ToViewModel(); 
      response.Message = "The invoices and revenue for this doctor were successfully updated."; 
     } 
     catch(Exception ex) 
     { 
      response.DidError = true; 
      response.Message = ex.Message; 
     } 
     return response.ToHttpResponse(); 
    } 

    //DELETE DoctorRevenue/5 
    [HttpDelete] 
    [Route("DoctorRevenue/{RecordId}")] 
    public async Task<IActionResult> DeleteDoctorRevenue(int RecordId) 
    { 
     var response = new SingleModelResponse<DocRevViewModel>() as ISingleModelResponse<DocRevViewModel>; 

     try 
     { 
      var entity = await Task.Run(() => 
      { 
       return DoctorRevenueRepository.DeleteDocRevenue(RecordId); 
      }); 
      response.Message = "This doctor's invoices and revenue have been deleted"; 
     } 
     catch(Exception ex) 
     { 
      response.DidError = true; 
      response.Message = ex.Message; 
     } 
     return response.ToHttpResponse(); 
    } 
} 

Startup.cs包括:

public void ConfigureServices(IServiceCollection services) 
    { 

     services.AddScoped<IDoctorMasterRepository, DoctorMasterRepository>(); 
     services.AddScoped<IDoctorRevenueRepository, DoctorRevenueRepository>(); 
     services.AddScoped<IFacilityMasterRepository, FacilityMasterRepository>(); 
     services.AddScoped<IFacilityRevenueRepository, FacilityRevenueRepository>(); 
     // Add framework services. 
     services.AddApplicationInsightsTelemetry(Configuration); 

     services.AddMvc(); 
     services.AddOptions(); 

     services.AddLogging(); 

     services.AddSingleton<IDoctorMasterRepository, DoctorMasterRepository>(); 
     services.AddSingleton<IFacilityMasterRepository, FacilityMasterRepository>(); 
     services.AddSingleton<IDoctorRevenueRepository, DoctorRevenueRepository>(); 
     services.AddSingleton<IFacilityRevenueRepository, FacilityRevenueRepository>(); 

     services.AddSwaggerGen(); 

    } 

    // 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.UseStaticFiles(); 

     app.UseDeveloperExceptionPage(); 

     app.UseApplicationInsightsRequestTelemetry(); 

     app.UseApplicationInsightsExceptionTelemetry(); 

     app.UseMvc(); 

     app.UseSwagger(); 

     app.UseSwaggerUi(); 
    } 

成功構建項目後,調試產生一個錯誤:

localhost refused to connect. ERR_CONNECTION_REFUSED 

如何解決這個問題,這樣我可以查看我的API文檔頁面?

+0

默認情況下[Route(「api/[controller]」)]是否映射到當前控制器?我從未見過如此有興趣知道 –

+0

的號碼是否需要在Stsrtup.cs中添加路由才能實現此目的? – SidC

+0

看看這裏http://www.hanselman.com/blog/ExploringAMinimalWebAPIWithASPNETCore.aspx,啓動方法中的綁定 –

回答

0

我發現dotnet run需要在命令行執行才能啓動本地IISExpress平臺。這樣做後,郵差按預期的方式服務我的API路線。

相關問題