ASP.NET 核心 Web API 抛出 HTTP 500

ASP.NET Core Web API Throws HTTP 500

我创建了一个新的 ASP.NET Core Web API 并且有几个控制器,例如这个:

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

成功构建项目后,Debug产生错误:

localhost refused to connect.  ERR_CONNECTION_REFUSED

我该如何补救才能查看我的 API 文档页面?

我发现需要在命令行执行dotnet run才能启动本地IISExpress平台。这样做之后,Postman 按预期提供了我的 API 路线。