1

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.

Unbreakable
  • 7,776
  • 24
  • 90
  • 171
  • 1
    is your server a proxy server and throwing 504 or are you using a proxy server? – Steve Jan 11 '17 at 16:09
  • I am just a beginner. Can you tell me how to find that. pardon my ignorance. – Unbreakable Jan 11 '17 at 16:10
  • What @Steve is trying to get to is that if your site has a front end proxy (like a load balancer), then that load balancer is the one "responding" to the user/browser (it's not your app/web site). So the question would be whether or not you have control over that proxy. Though it seems there is an application issue for it not to respond "in time" (whatever the timeout value is set in proxy). `user` <--> `load balancer/proxy` <---> your website/app ..Hth... – EdSF Jan 11 '17 at 18:44
  • No load balancer and all. Web site is hosted at IIS server and I have full access to IIS settings and all code is there at IIS. – Unbreakable Jan 11 '17 at 18:56
  • While making payment client gets this error. – Unbreakable Jan 11 '17 at 18:56
  • 1
    If that's a call to some external (payment API), then handle that in your call (your api client that calls the external payment api), without needing config changes that's meant for your _entire app_. In other words, keep it in the "payments scope" and handle it like a "failed payment confirmation/condition" (and work on messaging the user). Most payment APIs will have error codes and recommendations on how to handle them. Hth... – EdSF Jan 11 '17 at 20:21

2 Answers2

3

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.

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

Ralf Bönning
  • 14,515
  • 5
  • 49
  • 67
  • @Unbreakable config IIS – Steve Jan 11 '17 at 16:17
  • Any link please to how to configure for beginners? Really appreciate the help. – Unbreakable Jan 11 '17 at 16:18
  • @Unbreakable - I have edited my answer and posted this link https://www.youtube.com/watch?v=qTZho5FxPM8 which shows how to setup a custom error page in IIS. – Ralf Bönning Jan 11 '17 at 18:10
  • Thank you so much for the response. Actually my manager wants me to capture the error somehow in ASP.net code. And display an error message from there. I mean he does not want me to redirect to a page by making changes in IIS "Errro pages" Settings. Is it still possible to capture it in the ASP.net code and then display a message without making changes in IIS error pages in settings. I am allowed to make changes in IIS web config file We have some filters, registerglobalfilters, GlobalFilterCollection and all of that. Can you please guide me a little here. – Unbreakable Jan 11 '17 at 18:47
  • In the below answer provided by Andrew he has not mentioned to make changes in the IIS GUI Settings and add "504 error page" explicitly in the IIS Settings. Hope below answer in not incomplete. I will try that once. – Unbreakable Jan 11 '17 at 18:59
  • 1
    Because we are speaking of an 504 error (and not 404 or 500), it is not possible to capture it in ASP.NET code. The IIS is waiting for the ASP.NET Code for a response, which is not sent (DB slow or whatever the reason is). Therefore the IIS kicks in after waiting until a timeout is reached, sends an own error page (which can be configured) and closes the connection, while the request in ASP.NET code might still be busy. So there is no redirecting, the IIS simply closes the connection (to avoid polution caused by endless taking requests). – Ralf Bönning Jan 11 '17 at 20:25
  • I just saw the comment of EdSF. Please clarify your post, what do you mean by "getting a 504 status code". Is it your backend code making a call or is it the browser, which requests information from your application. – Ralf Bönning Jan 11 '17 at 20:30
  • @rboe: if I follow the video tutorial do I need to make any changes in my web config file and all. Video does tell anything about changing config file. So, in my asp.net mvc - 5 application, I am simply adding a html page at the root folder and adding its location in IIS Server under `504` error. that's it right. i do not need controller and all right? all i need is one html file. right? please guide me. – Unbreakable Jan 24 '17 at 14:28
  • and to answer your above query. I am getting this error in my browser. my browser requests something from backend code and then I am getting time out. – Unbreakable Jan 24 '17 at 14:31
0

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.

Community
  • 1
  • 1
Andrew Diamond
  • 6,295
  • 1
  • 15
  • 33
  • Can you kindly explain what is `redirect="~/404Redirect/"` in the answer to the link you posted. do I need to make one controller and one view. or just the view would suffice? – Unbreakable Jan 11 '17 at 16:27
  • Youll need a controller if you want MVC to handle it. I changed the link to a better one, check this one out. – Andrew Diamond Jan 11 '17 at 16:51
  • I will try it and will get back to you. Just one small thing. Do, I need too make any changes to IIS or something. I will change the config file, add the controller and will add the views accordingly. Do I need to open IIS Server and do any modifications. – Unbreakable Jan 11 '17 at 17:18
  • By changing the config file, you are changing IIS. IIS is mainly just a GUI for these config files – Andrew Diamond Jan 11 '17 at 17:44
  • @AndrewDiamond - IIS is *much* more than "just a GUI for these config files"..... – Tim Jan 11 '17 at 18:16
  • @Tim - Correct, IIS is the actual server too. But everything the configuration part of IIS does can be done in the *.config file. My gaff. – Andrew Diamond Jan 11 '17 at 18:53
  • Andrew, Just to make sure I understood you properly, I do not need to make any changes in IIS GUI right? I mean I do not need to go to IIS Server settings and under "Error Pages" I do not need to add any error pages right for status 504? So, I am trying what you have suggested but I am not going to make any changes in the IIS Server GUI "Error Pages" as explained in this video. "https://www.youtube.com/watch?v=qTZho5FxPM8" – Unbreakable Jan 11 '17 at 19:03
  • I am a new guy so asking all these question. Bear with me. – Unbreakable Jan 11 '17 at 19:03
  • I am just going to alter "config file", "add controller" and add "view page" :) – Unbreakable Jan 11 '17 at 19:04