C# ASP.NET Core Web API incorrectly returning 403 on remote server

30 Views Asked by At

When I debug locally and run GetEntityReport with no results, I get a http 404, which is great. When I post this to the remote server the logs report 404, but 403 seems to be returned...which is not great.

Here is my controller, I only included the function that is causing an issue:

namespace ReportServices.Controllers
{
    private readonly IReportServiceRepo _repository;
    private readonly IMapper _mapper;
    private readonly ILogger<ReportServicesController> _logger;

    public ReportServicesController(IReportServiceRepo repository, IMapper mapper, ILogger<ReportServicesController> logger)
    {
        _repository = repository;
        _mapper = mapper;
    }

    // GET api/reportservice/entities/{id}/2022/11/test
    [AllowAnonymous]
    [HttpGet]
    [Route("entities/{entityId:int}/{year:int:maxlength(4)}/{month:int:maxlength(2)}/{title}", Name = "GetEntityReport")]
    [ProducesResponseType(typeof(IList<ReportReadSimpleDto>), StatusCodes.Status200OK)]
    [ProducesResponseType(StatusCodes.Status404NotFound)]
    public ActionResult<IList<ReportReadSimpleDto>> GetEntityReport(int entityId, int year, int month, string title)
    {
        var reportsItem = _repository.SearchForReport(entityId, year, month, title);

        if (reportsItem != null && reportsItem.Count > 0)
        {
            return Ok(_mapper.Map<IEnumerable<ReportReadSimpleDto>>(reportsItem));
        }

        return NotFound("No report found for this criteria.");
    }
}

Log stuff:

Request starting HTTP/1.1 GET              
    https://www.skagitcounty.net/Apps/REST/ReportService/api/reportservice/entities/1/2021/1/test

Request finished HTTP/1.1 GET         
    https://www.skagitcounty.net/Apps/REST/ReportService/api/reportservice/entities/1/2021/1/test - - - 404 - text/plain;+charset=utf-8 38.6428ms 

This is a screenshot of Swagger query (https://i.stack.imgur.com/4v3xX.png)

The hosting bundle is installed on the remote server and is .NET 8. Could that be causing this? Maybe an IIS setting?

Any ideas?

Thank you in advance!

If I use a query that has a resulting object, the result is perfect (200 OK). Weird.

1

There are 1 best solutions below

0
user5145 On

This was all due to custom error handling in a parent site.

Adding something like:

<httpErrors errorMode="DetailedLocalOnly">
<clear />
</httpErrors>

Fixed my issue.