.NET Core Web API show HTML message when no route matches

ASP.NET core

Add this to the middleware pipeline


app.UseStatusCodePages(new StatusCodePagesOptions()
{
    
    HandleAsync = (ctx) =>
    {
        if (ctx.HttpContext.Response.StatusCode == 404)
        {
            ctx.HttpContext.Response.ContentType = Text.Html;

            ctx.HttpContext.Response.WriteAsync(
                    $"<div style='text-align: center'><h1 style='margin-top: 100px'>My Server Is Working</h1></div>");
        }

        return Task.FromResult(0);
    }
});

Post a Comment

Previous Post Next Post