简体   繁体   中英

Azure “500 - The request timed out” custom error page

We have deployed an ASP.NET MVC applicaton to an Azure App Service.

If a request takes longer than 230 seconds Azure returns an error page "500 - The request timed out. The web server failed to respond within the specified time." - https://stackoverflow.com/a/38676086

We are making changes to ensure less processing is done on the Web request, however there may still be a chance that these limits are hit. Does anyone know if there is a way to configure a custom error page to be shown instead of the generic error page shown below?

Azure 500-请求超时页面

This article should help you, remember that a custom error only works if the request reach the website. http://benfoster.io/blog/aspnet-mvc-custom-error-pages

We are making changes to ensure less processing is done on the Web request, however there may still be a chance that these limits are hit. Does anyone know if there is a way to configure a custom error page to be shown instead of the generic error page shown below?

Since you could not overcome the limitation about request timeout (230 second) in Azure App Service, you need to either optimize your server-side processing or you need to catch the exception and customize the response. I tried to capture the exception by using Application_Error event and custom HTTP Modules , but failed in the end.

For a workaround, you could specify the timeout for your action and capture the exception in your application before the timeout exception thrown by Azure App Service, then you could add your custom error page. You could leverage AsyncTimeoutAttribute and define your action as follows:

[AsyncTimeout(duration:10000)] //The timeout value in milliseconds.
[HandleError(ExceptionType = typeof(TimeoutException),
                            View = "TimeoutError")]
public async Task<ActionResult> LongTask(CancellationToken cancellationToken)
{
    await Task.Delay(TimeSpan.FromSeconds(20), cancellationToken);
    return Content("Done");
}

Note: HandleErrorAttribute requires customErrors to be enabled in your Web.Config. If the TimeoutException happens, then you would be redirected to the particular MVC view you defined.

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