Copied to clipboard

Flag this post as spam?

This post will be reported to the moderators as potential spam to be looked at


  • Paul Aikman 43 posts 97 karma points
    Jun 06, 2019 @ 20:31
    Paul Aikman
    0

    Global.asax Application_Error called twice?

    Hi folks, got a weird issue in our Umbraco 7.x app where the Application_Error method is called twice following an exception.

    Is there a recommended way to capture application errors within the Global.asax? I also tried implementing the OnApplicationError event, but it never seemed to be hit when I triggered an exception in the app.

  • Brandon Osborne 37 posts 160 karma points c-trib
    Jun 07, 2019 @ 03:13
    Brandon Osborne
    0

    Hiya Paul,

    I had some problems in the distant past getting custom errors working in Umbraco. I never got it to work quite right using the Global.asax, so I took the following approach:

    1. Make the following modifications to your web.config

      <system.web><customErrors mode="On" redirectMode="ResponseRewrite" defaultRedirect="~/Oops"> <error redirect="~/Oops?statusCode=404" statusCode="404" /> <error redirect="~/Oops?statusCode=500" statusCode="500" /> </customErrors></system.web>

      <system.webServer> <httpErrors existingResponse="Auto" errorMode="Custom"> <remove statusCode="404" subStatusCode="-1" /> <remove statusCode="500" subStatusCode="-1" /> <error statusCode="404" path="/Oops?statusCode=404" responseMode="ExecuteURL" /> <error statusCode="500" path="/Oops?statusCode=500" responseMode="ExecuteURL" /> </httpErrors> </system.webServer>

    2. My Error.cshtml partial looks like this:

    @inherits Umbraco.Web.Macros.PartialViewMacroPage <section class="container" id="ErrorSection"> <div class="row"> <div class="col-xs-12"> <table cellspacing="5" cellpadding="3" style="background-color: #fff;" class="table-responsive" width="400" > <tbody> <tr> <td valign="top" align="left" id="tableProps"> <img width="25" height="33" src="~/Images/PageError.gif" id="pagerrorImg"> </td> <td width="360" valign="middle" align="left" id="tableProps2"> <h1 style="COLOR: black; FONT: 13pt/15pt verdana" id="errortype"><span id="errorText">@Response.Status</span></h1> </td> </tr> <tr> <td width="400" colspan="2" id="tablePropsWidth"> <font style="COLOR: black; FONT: 8pt/11pt verdana">Possible causes:</font> </td> </tr> <tr> <td width="400" colspan="2" id="tablePropsWidth2"> <font style="COLOR: black; FONT: 8pt/11pt verdana" id="LID1"> <hr color="#C0C0C0" noshade> <ul> <li id="list1"> <span class="infotext"> <strong>Baptist explanation: </strong>There must be sin in your life. Everyone else opened it fine.<br> </span> </li> <li> <span class="infotext"> <strong>Presbyterian explanation: </strong>It's not God's will for you to open this link.<br> </span> </li> <li> <span class="infotext"> <strong> Word of Faith explanation:</strong> You lack the faith to open this link. Your negative words have prevented you from realizing this link's fulfillment.<br> </span> </li> <li> <span class="infotext"> <strong>Charismatic explanation: </strong>Thou art loosed! Be commanded to OPEN!<br> </span> </li> <li> <span class="infotext"> <strong>Unitarian explanation:</strong> All links are equal, so if this link doesn't work for you, feel free to experiment with other links that might bring you joy and fulfillment.<br> </span> </li> <li> <span class="infotext"> <strong>Buddhist explanation:</strong> .........................<br> </span> </li> <li> <span class="infotext"> <strong>Episcopalian explanation:</strong> Are you saying you have something against homosexuals?<br> </span> </li> <li> <span class="infotext"> <strong>Christian Science explanation: </strong>There really is no link.<br> </span> </li> <li> <span class="infotext"> <strong>Atheist explanation: </strong>The only reason you think this link exists is because you needed to invent it.<br> </span> </li> <li> <span class="infotext"> <strong>Church counselor's explanation:</strong> And what did you feel when the link would not open? </span> </li> </ul> <p> <br> </p> <h2 style="font: 8pt/11pt verdana; color: black" id="ietext"> <img width="16" height="16" align="top" src="~/Images/Search.gif"> HTTP @Response.StatusCode - @Response.StatusDescription <br> </h2> </font> </td> </tr> </tbody> </table> </div> </div> </section>

    3) ~/Oops is just a very basic Umbraco page with page title & body content. The macro from step 2 is the only thing in the body content.

    4) Lastly, you'll want to update your ~/config/umbracoSettings.config file with the following:

    <content>
        <errors>
          <error404>[the pageId of the ~/Oops page]</error404>
          <error500>7100</error500>
       </errors>
    </content>
    

    This should only execute once. I'm sure you want to do more than just display the error to the user. You can either write code in the Razor of the partial to execute whatever logic you need to execute, or you could just as easily point your redirects in the web.config (step 1) to a controller action. I use elmah to notify me of all errors and I don't really care about much else, so the above was perfect for me. Let me know if you have any questions.

Please Sign in or register to post replies

Write your reply to:

Draft