简体   繁体   中英

ASP.NET Core Web API Throws HTTP 500

I've created a new ASP.NET Core Web API and have several controllers such as this one:

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

My Startup.cs includes:

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

After successfully building the project, a Debug produces an error:

localhost refused to connect.  ERR_CONNECTION_REFUSED

How do I remedy this such that I can view my API documentation page?

I found that dotnet run needed to be executed at the command line to start the local IISExpress platform. After doing so, Postman served my API routes as expected.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM