Show custom error for “504 Gateway Timeout error”

2019-04-16 09:40发布

问题:

I am working to ASP.net MVC-5 project and I am a beginner. As of now I know the basics of MVC and able to display pages and make post and get request. But I am stuck with one issue where I get "504 gateway time out error" and I need to implement one functionality where if the client gets `504 gateway timeout error" I should be able to show a particular page like "Something is wrong" instead of standard error. To start with below is what I did.

  <system.web>
    <compilation debug="true" targetFramework="4.5.1" />
    <httpRuntime targetFramework="4.5" />
    <customErrors mode="On" redirectMode="ResponseRewrite">
    </customErrors>
  </system.web>

But what else I need to change or add. Can someone guide me here. I am lost.

回答1:

If your ASP.NET MVC application is hosted by the IIS (or another proxy server), then the 504 error is returned by the IIS and sent to the browser, because your application does not respond to the request in a timely manner. The IIS has got a timeout (configurable for each web site) and if this is reached, then 504 is returned.

In this scenario you cannot define a custom error page inside your application, because it is not reacting.

Yout can configure the IIS to server custom error pages. This video tutorial shows how to do this.



回答2:

ASP doesn't actually return those pages, that's actually IIS.

Check out this SO on configuring IIS to let you return custom error pages


Basically you'll need to configure these settings:

<system.web>
    <customErrors mode="On" defaultRedirect="~/Error">
      <!--Use whatever paths you need here, along with the accompanying status code -->
      <error redirect="~/Error/NotFound" statusCode="404" />
    </customErrors>
</system.web>

And create a controller to handle the errors, like so:

public class ErrorController : Controller
{
    public ViewResult Index()
    {
        return View();
    }
    public ViewResult NotFound()
    {
        Response.StatusCode = 404;  
        return View();
    }
}

Along with whatever view you need to match the Action.