Application_Error custom error handler in Global.asax












0














I want to detect and redirect custom errors from Global.asax. It returns 200 ok or null. I am not sure how can i detect and redirect to errors automatically. I hope you guys have an idea.



In Web.Config



<system.web>
<customErrors mode="On" />
</system.web>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>


In Global.asax



protected void Application_Error(object sender, EventArgs e)
{
Response.TrySkipIisCustomErrors = true;

Exception ex = Server.GetLastError();

if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 200)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 400)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 500)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
Server.ClearError();
}









share|improve this question
























  • i'd take a look at this stackoverflow.com/questions/8944355/…
    – Peter Karman
    Nov 12 '18 at 16:22










  • I'm using webforms @PeterKarman
    – Mert
    Nov 12 '18 at 16:27












  • ah how about plain old Response.Redirect then? Something like Response.Redirect("/error.aspx"). Also, it seems like you don't need do the various checks you are doing as you are always redirecting to error. Just redirecting in the method should be sufficient
    – Peter Karman
    Nov 12 '18 at 21:41










  • Thanks, i can handle error code '400' but i can't handle 404 not found.. do you have an idea why? @PeterKarman
    – Mert
    Nov 13 '18 at 16:04


















0














I want to detect and redirect custom errors from Global.asax. It returns 200 ok or null. I am not sure how can i detect and redirect to errors automatically. I hope you guys have an idea.



In Web.Config



<system.web>
<customErrors mode="On" />
</system.web>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>


In Global.asax



protected void Application_Error(object sender, EventArgs e)
{
Response.TrySkipIisCustomErrors = true;

Exception ex = Server.GetLastError();

if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 200)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 400)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 500)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
Server.ClearError();
}









share|improve this question
























  • i'd take a look at this stackoverflow.com/questions/8944355/…
    – Peter Karman
    Nov 12 '18 at 16:22










  • I'm using webforms @PeterKarman
    – Mert
    Nov 12 '18 at 16:27












  • ah how about plain old Response.Redirect then? Something like Response.Redirect("/error.aspx"). Also, it seems like you don't need do the various checks you are doing as you are always redirecting to error. Just redirecting in the method should be sufficient
    – Peter Karman
    Nov 12 '18 at 21:41










  • Thanks, i can handle error code '400' but i can't handle 404 not found.. do you have an idea why? @PeterKarman
    – Mert
    Nov 13 '18 at 16:04
















0












0








0







I want to detect and redirect custom errors from Global.asax. It returns 200 ok or null. I am not sure how can i detect and redirect to errors automatically. I hope you guys have an idea.



In Web.Config



<system.web>
<customErrors mode="On" />
</system.web>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>


In Global.asax



protected void Application_Error(object sender, EventArgs e)
{
Response.TrySkipIisCustomErrors = true;

Exception ex = Server.GetLastError();

if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 200)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 400)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 500)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
Server.ClearError();
}









share|improve this question















I want to detect and redirect custom errors from Global.asax. It returns 200 ok or null. I am not sure how can i detect and redirect to errors automatically. I hope you guys have an idea.



In Web.Config



<system.web>
<customErrors mode="On" />
</system.web>
<system.webServer>
<httpErrors existingResponse="PassThrough"/>
</system.webServer>


In Global.asax



protected void Application_Error(object sender, EventArgs e)
{
Response.TrySkipIisCustomErrors = true;

Exception ex = Server.GetLastError();

if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 200)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 400)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 404)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else if (ex is HttpException && ((HttpException)ex).GetHttpCode() == 500)
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
else
{
errormessage = ex.Message;
Response.RedirectToRoute("error");
}
Server.ClearError();
}






c# asp.net error-handling webforms global-asax






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 12 '18 at 15:21

























asked Nov 12 '18 at 15:06









Mert

226




226












  • i'd take a look at this stackoverflow.com/questions/8944355/…
    – Peter Karman
    Nov 12 '18 at 16:22










  • I'm using webforms @PeterKarman
    – Mert
    Nov 12 '18 at 16:27












  • ah how about plain old Response.Redirect then? Something like Response.Redirect("/error.aspx"). Also, it seems like you don't need do the various checks you are doing as you are always redirecting to error. Just redirecting in the method should be sufficient
    – Peter Karman
    Nov 12 '18 at 21:41










  • Thanks, i can handle error code '400' but i can't handle 404 not found.. do you have an idea why? @PeterKarman
    – Mert
    Nov 13 '18 at 16:04




















  • i'd take a look at this stackoverflow.com/questions/8944355/…
    – Peter Karman
    Nov 12 '18 at 16:22










  • I'm using webforms @PeterKarman
    – Mert
    Nov 12 '18 at 16:27












  • ah how about plain old Response.Redirect then? Something like Response.Redirect("/error.aspx"). Also, it seems like you don't need do the various checks you are doing as you are always redirecting to error. Just redirecting in the method should be sufficient
    – Peter Karman
    Nov 12 '18 at 21:41










  • Thanks, i can handle error code '400' but i can't handle 404 not found.. do you have an idea why? @PeterKarman
    – Mert
    Nov 13 '18 at 16:04


















i'd take a look at this stackoverflow.com/questions/8944355/…
– Peter Karman
Nov 12 '18 at 16:22




i'd take a look at this stackoverflow.com/questions/8944355/…
– Peter Karman
Nov 12 '18 at 16:22












I'm using webforms @PeterKarman
– Mert
Nov 12 '18 at 16:27






I'm using webforms @PeterKarman
– Mert
Nov 12 '18 at 16:27














ah how about plain old Response.Redirect then? Something like Response.Redirect("/error.aspx"). Also, it seems like you don't need do the various checks you are doing as you are always redirecting to error. Just redirecting in the method should be sufficient
– Peter Karman
Nov 12 '18 at 21:41




ah how about plain old Response.Redirect then? Something like Response.Redirect("/error.aspx"). Also, it seems like you don't need do the various checks you are doing as you are always redirecting to error. Just redirecting in the method should be sufficient
– Peter Karman
Nov 12 '18 at 21:41












Thanks, i can handle error code '400' but i can't handle 404 not found.. do you have an idea why? @PeterKarman
– Mert
Nov 13 '18 at 16:04






Thanks, i can handle error code '400' but i can't handle 404 not found.. do you have an idea why? @PeterKarman
– Mert
Nov 13 '18 at 16:04














1 Answer
1






active

oldest

votes


















0














Well,



I found temporary solution for my problem. It's not the best but you may need it.



In Web.Config



<system.web>
<customErrors mode="On" /> // On or Off doesn't matter ISS 7 or newer version
</system.web>
<system.webServer>
<httpErrors existingResponse="Auto">
<clear/>
<error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
</httpErrors>
</system.webServer>


In Global.asax



HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
string ex_message = httpexception.InnerException;
// send or save errors here
Server.ClearError();
Response.Redirect("error");


Note: I didn't handle 404 error from codebehind, web.configuration helps about it.






share|improve this answer





















    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264933%2fapplication-error-custom-error-handler-in-global-asax%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    Well,



    I found temporary solution for my problem. It's not the best but you may need it.



    In Web.Config



    <system.web>
    <customErrors mode="On" /> // On or Off doesn't matter ISS 7 or newer version
    </system.web>
    <system.webServer>
    <httpErrors existingResponse="Auto">
    <clear/>
    <error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
    </httpErrors>
    </system.webServer>


    In Global.asax



    HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
    string ex_message = httpexception.InnerException;
    // send or save errors here
    Server.ClearError();
    Response.Redirect("error");


    Note: I didn't handle 404 error from codebehind, web.configuration helps about it.






    share|improve this answer


























      0














      Well,



      I found temporary solution for my problem. It's not the best but you may need it.



      In Web.Config



      <system.web>
      <customErrors mode="On" /> // On or Off doesn't matter ISS 7 or newer version
      </system.web>
      <system.webServer>
      <httpErrors existingResponse="Auto">
      <clear/>
      <error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
      </httpErrors>
      </system.webServer>


      In Global.asax



      HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
      string ex_message = httpexception.InnerException;
      // send or save errors here
      Server.ClearError();
      Response.Redirect("error");


      Note: I didn't handle 404 error from codebehind, web.configuration helps about it.






      share|improve this answer
























        0












        0








        0






        Well,



        I found temporary solution for my problem. It's not the best but you may need it.



        In Web.Config



        <system.web>
        <customErrors mode="On" /> // On or Off doesn't matter ISS 7 or newer version
        </system.web>
        <system.webServer>
        <httpErrors existingResponse="Auto">
        <clear/>
        <error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
        </httpErrors>
        </system.webServer>


        In Global.asax



        HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
        string ex_message = httpexception.InnerException;
        // send or save errors here
        Server.ClearError();
        Response.Redirect("error");


        Note: I didn't handle 404 error from codebehind, web.configuration helps about it.






        share|improve this answer












        Well,



        I found temporary solution for my problem. It's not the best but you may need it.



        In Web.Config



        <system.web>
        <customErrors mode="On" /> // On or Off doesn't matter ISS 7 or newer version
        </system.web>
        <system.webServer>
        <httpErrors existingResponse="Auto">
        <clear/>
        <error statusCode="404" path="/notfound.aspx" responseMode="ExecuteURL" />
        </httpErrors>
        </system.webServer>


        In Global.asax



        HttpException httpexception = Server.GetLastError().GetBaseException() as HttpException;
        string ex_message = httpexception.InnerException;
        // send or save errors here
        Server.ClearError();
        Response.Redirect("error");


        Note: I didn't handle 404 error from codebehind, web.configuration helps about it.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 17:25









        Mert

        226




        226






























            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.





            Some of your past answers have not been well-received, and you're in danger of being blocked from answering.


            Please pay close attention to the following guidance:


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53264933%2fapplication-error-custom-error-handler-in-global-asax%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            Florida Star v. B. J. F.

            Error while running script in elastic search , gateway timeout

            Adding quotations to stringified JSON object values