Global Exception Error

Jagjit Saini 106 Reputation points
2021-07-13T11:40:43.973+00:00

Hi

I have Index file in Shared folder. Controller Name is Error. Index file in Shared folder is Error.cshtml
What change i need to make in this line - Response.Redirect(string.Format("~/Error/Index/?message={1}", "", HttpUtility.UrlEncode(exception.Message)));

protected void Application_Error(object sender, EventArgs e)

{

Exception exception = Server.GetLastError();

if (exception != null)

{

HttpException httpException = null;

if (exception is HttpException)

{

httpException = exception as HttpException;

}

Response.Clear();

Server.ClearError();

Response.Redirect(string.Format("~/Error/Index/?message={1}", "", HttpUtility.UrlEncode(exception.Message)));

}

}

Thanks

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,542 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Michael Taylor 55,501 Reputation points
    2021-07-13T13:53:55.847+00:00

    That's not how you do error handling in MVC. In MVC you should register a global exception filter as documented here for ASP.NET. This will likely solve the problem you're having.

    You didn't really explain what problem you're actually having. The path looks reasonable other than the fact that your String.FOrmat call is using an ordinal of 1 but you have only 1 actual argument. Provided your ErrorController class has an action called Index and inside that action you eventually call return View("Error") then it'll render properly.

       String.Format("~/error/index?message={0}", HttpUtility.UrlEncode(exception.Message));  
    

    Note that MVC provides the Url property on a controller (which is backed by a type) to give you back the URL to an action without you having to manually encode it. I'm just not sure it can be used this low level. The property isn't available because you aren't in a controller but the underlying type could still work. Never tried it since this is non-standard error handling anyway.

    Besides a global exception filter I'll also point out that the default ASP.NET template already provides an error page for you and is configured to be used. Having an error controller is overkill. In general you have a single Error.cshtml view that you can customize, no controller to back it and let the framework do its thing.

    0 comments No comments

  2. Yijing Sun-MSFT 7,086 Reputation points
    2021-07-14T06:51:19.253+00:00

    Hi @Jagjit Saini ,
    Your parameter's index have wrong.You need do like this:

    Response.Redirect(string.Format("~/Error/Index/?message={0}", "", HttpUtility.UrlEncode(exception.Message)));  
    

    By the way,there are some tradeoffs with your approach. Be very very careful with looping in this kind of error handling.This can be an issue (performance) for heavily used systems.
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.