Skip to content

JsonExceptionMiddleware in ASP.NET Core 3  #17

@rochapablo

Description

@rochapablo

I'm trying to follow the example, but it seems that app.UseExceptionHandler it's called only when I remove app.UseAuthorization, but I do need UseAuthorization. What could I be missing?

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    // Serialize all exceptions to JSON
    var jsonExceptionMiddleware = new JsonExceptionMiddleware(app.ApplicationServices.GetRequiredService<Microsoft.AspNetCore.Hosting.IHostingEnvironment>());
    app.UseExceptionHandler(new ExceptionHandlerOptions { ExceptionHandler = jsonExceptionMiddleware.Invoke });

    app.UseHsts();

    app.UseRouting();

    app.UseHttpsRedirection();            

    app.UseCors(x => x
        .AllowAnyOrigin()
        .AllowAnyMethod()
        .AllowAnyHeader());            

    app.UseAuthentication();

    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllers();
    });            

    app.UseResponseCaching();
}

When I have status 401 for example, It should be calling JsonExceptionMiddleware.Invoke before the request response, but the body it's empty.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions