Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Response.TrySkipIisCustomErrors equivalent for asp.net core?

Tags:

asp.net-core

How to prevent IIS from overriding custom error pages with IIS default error pages? Is there a Response.TrySkipIisCustomErrors equivalent for asp.net core? In ASP Net MVC I use the code below to send error without a custom page but in asp net core it is not working.

try
{
    // some code
}
catch (Exception ex)
{
    Response.TrySkipIisCustomErrors = true;
    Response.StatusCode = (int)HttpStatusCode.InternalServerError;
    mensagem = ex.Message;
}
like image 847
Marco Antonio Quintal Avatar asked Mar 14 '18 03:03

Marco Antonio Quintal


People also ask

Is there an equivalent of httpresponseexception in ASP NET Core?

ASP.NET Core doesn't include an equivalent type. Support for HttpResponseException can be added with the following steps: The preceding filter specifies an Order of the maximum integer value minus 10. This Order allows other filters to run at the end of the pipeline.

When should I set the tryskipiiscustomerrors property?

You can set this TrySkipIisCustomErrors property anytime before the Response gets sent out to IIS - ie. anytime before any output is sent explicitly. If you use Response.TrySkipIisCustomErrors alone you'll find that even with the flag set to true, the response still fires IIS errors rather than the content you return.

How to return status code from ASP NET Core API?

In ASP.NET Core, returning status code is easier than you might think. HTTP response status codes have so much importance in REST API’s. In any case if you want to return a status code from your ASP.NET Core API, all you have to do is in your controller method,

What is response compression in ASP NET Core?

Response compression in ASP.NET Core. Network bandwidth is a limited resource. Reducing the size of the response usually increases the responsiveness of an app, often dramatically. One way to reduce payload sizes is to compress an app's responses.


1 Answers

It sounds like you're trying to disable the status code error pages for a particular request. The ASP.NET Core documentation for Handling Errors gives you the answer.

It's not as succinct as the old TrySkipIisCustomErrors flag, but may be clearer what's actually going on:

var statusCodePagesFeature =
    HttpContext.Features.Get<IStatusCodePagesFeature>();

if (statusCodePagesFeature is not null)
{
    statusCodePagesFeature.Enabled = false;
}
like image 119
decates Avatar answered Oct 09 '22 00:10

decates