What is part of an unhandled exception handling strategy in asp.net mvc applications?

I would like to learn some of the strategies / practices that you deal with in order to handle unhandled exceptions in ASP.NET MVC.

In short, I want to avoid the yellow screen whenever an error occurs, and show the visitor an error message.

I mean, are you writing a controller for this that shows the corresponding error page, or you go in another way, how to write an httpmodule and capture the error globally.

Any inputs in this direction are appreciated.

+3
source share
3 answers

HandleError - . , Ajax JQuery, ExtJs .

public class DataController : Controller
{    
    [HandleError(ExceptionType = typeof(ArgumentException), View = "ErrorAjax")]
    public void Foo(string x, string y)
    {    
        if (String.IsNullorEmpty(x))
            throw new ArgumentException("String cannot be empty!");

            // Call your layers or whatever here
            AnotherCall();
    }
}

(ErrorAjax). , (HandleErrorInfo)

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<HandleErrorInfo>" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Sorry Dude!</title>
</head>
<body>
    <div>
        <!-- be creative here-->
        Sorry, an error occurred while processing your request.
        Action = <%= ViewData.Model.ActionName %>
        Controller = <%= ViewData.Model.ControllerName %>
        Message = <%= ViewData.Model.Exception.Message %>

    </div>
</body>
</html>

  • web.config , customErrors mode = "On"
+7
+4

, . , HandleError, . HandleError. 4.

+4

Source: https://habr.com/ru/post/1702542/


All Articles