Asp.Net MVC how to get error code

I am using asp.net mvc and the code below to return a description of the exception and store it in the session. I need help finding an exception error code such as 500 403 404, etc. How can i do this?

This is how I save the error message in the session. System.Web.HttpContext.Current.Session ["errorMessage"] = filterContext.Exception.Message.ToString ();

public override void OnException(System.Web.Mvc.ExceptionContext filterContext)
        {
            ProcessResult processResult = new ProcessResult();

            //If the exeption is already handled we do nothing
            if (filterContext.ExceptionHandled)
            {
                return;
            }
            else
            {
                var controllerName = (string)filterContext.RouteData.Values["controller"];
                var actionName = (string)filterContext.RouteData.Values["action"];
                var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);
                processResult.Controller = controllerName;
                processResult.Action = actionName;
                processResult.ExceptionMessage = filterContext.Exception.Message;
                System.Web.HttpContext.Current.Session["errorMessage"] =                      filterContext.Exception.Message.ToString();
                processResult.StackTrace = filterContext.Exception.StackTrace;
                processResult.Source = filterContext.Exception.Source;

            }
            //Mark the exception as handled
            filterContext.ExceptionHandled = true;
        }
    }
+4
source share
1 answer

Check if the exception is an HttpException. Then discard it as such and take the status code.

if (filterContext.Exception is HttpException)
{
    var statusCode = ((HttpException)filterContext.Exception).GetHttpCode();
}
+7
source

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


All Articles