在MVC部署的应用程序中显示错误页面

问题描述:

嗨朋友们,



我正在处理一个MVC应用程序,每当发生错误时它应该重定向到错误页面。

在我的本地计算机上运行时,它按预期工作。

但是在部署的解决方案中,它抛出web.config警告以更改自定义错误模式=关闭,以便您可以查看类似的错误那个。



有没有解决上述问题的解决方案?

我想要的是一旦出现错误我需要重定向到错误页面发生在申请中。



谢谢,

RK

Hi Friends,

I am working on a MVC application, here whenever the error occurred it should redirected to error page.
While running in my local machine it works as expected.
But when in deployed solution, its throwing web.config warning to change the "Custom error mode=off" so that you can view the error something like that.

Is there any solution to solve this above issue?
What I want is I need to redirect to error page once error is occurred in the application.

Thanks,
RK

HI ,

创建你的错误控制器和视图。

然后,在< system.web>中的web.config中。部分添加




create your error controler and view.
then, in web.config in <system.web> section add

<customErrors mode="On">
      <error statusCode="404" redirect="~/Error/Error"/>
    </customErrors>


请求请按照步骤操作。这是自定义错误处理,可帮助您在错误页面中显示异常

创建一个控制器并将其命名为错误控制器,操作结果如下

Please follow the steps.this is the custom error handling which helps you display the exception in your error page
Create a Controller and named it as Error controller and the actionresults like below
public class ErrorController : Controller
    {
        public ActionResult Index()
        {
            var error = ViewData.Model;
            return View(error);
        }

    }





通过右键单击索引视图创建视图



Create view by right clicking the index view

@model HandleErrorInfo
@{
    ViewBag.Title = "Server Error";
}




<h2 class="errorh">Server Error</h2>
<div>Controller: @Model.ControllerName</div>
<div>Action Name : @Model.ActionName</div>
<div>Exception: @Model.Exception.Message</div>
<div id="moreDetails">
<div>Stack Trace: @Model.Exception.StackTrace</div>
<div>Inner Exception: @Model.Exception.InnerException</div>
</div>





将以下内容放在Global的application_error方法中。 asax文件



put the below in below in application_error method in the Global.asax file

protected void Application_Error(object sender, EventArgs e)
       {
           var httpContext = ((MvcApplication)sender).Context;
           var currentController = " ";
           var currentAction = " ";
           var currentRouteData = RouteTable.Routes.GetRouteData(new HttpContextWrapper(httpContext));

           if (currentRouteData != null)
           {
               if (currentRouteData.Values["controller"] != null && !String.IsNullOrEmpty(currentRouteData.Values["controller"].ToString()))
               {
                   currentController = currentRouteData.Values["controller"].ToString();
               }

               if (currentRouteData.Values["action"] != null && !String.IsNullOrEmpty(currentRouteData.Values["action"].ToString()))
               {
                   currentAction = currentRouteData.Values["action"].ToString();
               }
           }

           var ex = Server.GetLastError();
           var controller = new ErrorController();
           var routeData = new RouteData();
           var action = "Index";

           if (ex is HttpException)
           {
               var httpEx = ex as HttpException;

               switch (httpEx.GetHttpCode())
               {
                   case 404:
                       action = "NotFound";
                       break;

                   case 401:
                       action = "AccessDenied";
                       break;
               }
           }

           httpContext.ClearError();
           httpContext.Response.Clear();
           httpContext.Response.StatusCode = ex is HttpException ? ((HttpException)ex).GetHttpCode() : 500;
           httpContext.Response.TrySkipIisCustomErrors = true;

           routeData.Values["controller"] = "Error";
           routeData.Values["action"] = action;

           controller.ViewData.Model = new HandleErrorInfo(ex, currentController, currentAction);
           ((IController)controller).Execute(new RequestContext(new HttpContextWrapper(httpContext), routeData));
       }



希望这有帮助


Hope this helps