Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 在错误视图MVC中显示特定控制器、活动、异常#_C#_Asp.net Mvc_Asp.net Mvc 4 - Fatal编程技术网

C# 在错误视图MVC中显示特定控制器、活动、异常#

C# 在错误视图MVC中显示特定控制器、活动、异常#,c#,asp.net-mvc,asp.net-mvc-4,C#,Asp.net Mvc,Asp.net Mvc 4,我确实为MVC应用程序创建了一个错误视图,它非常简单,但我可以设法显示控制器、活动和异常发生的消息(出于开发目的,我需要它),但它总是在代码中抛出异常。这是我的global.asax public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new HandleErrorAttribute()); }

我确实为MVC应用程序创建了一个错误视图,它非常简单,但我可以设法显示控制器、活动和异常发生的消息(出于开发目的,我需要它),但它总是在代码中抛出异常。这是我的global.asax

   public static void RegisterGlobalFilters(GlobalFilterCollection filters)
        {
            filters.Add(new HandleErrorAttribute());
        }

        protected void Application_Error(object sender, EventArgs e)
        {
            Exception exc = Server.GetLastError();
            Server.ClearError();
            Response.Redirect("/ErrorPage/ErrorMessage");
        }
这是我的ErrorPageController

 public class ErrorPageController : Controller
    {
        public ActionResult ErrorMessage()
        {
            return View();
        }
    }
这是抛出错误的视图,它在@Model.ControllerName、@Model.ActionName和@Model.Exception.Message中抛出错误

@model System.Web.Mvc.HandleErrorInfo 
<div class="container">

    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div>
                <br />
                <div class="form-group">
                    <div class="row">
                        <div class="col-md-12">
                            <img src="~/Imagenes/logo.png" class="img-responsive center-block" />
                        </div>
                    </div>
                    <h2>Ooops an error has been triggered</h2>
                    <p>Controller = @Model.ControllerName</p>
                    <p>Action = @Model.ActionName</p>
                    <p>Message = @Model.Exception.Message</p>
                </div>
                @*<hr />*@
                <br />


                <div class="form-group">
                    <div class="col-md-12">
                        <a href="@Url.Action("TipoEvento", "Home")" class="pull-right linkedin-link">Regresar <i class="fa fa-angle-right"></i></a>
                    </div>
                </div>

            </div>

        </div>
    </div>
</div>
@model System.Web.Mvc.HandleErrorInfo

哦,有一个错误被触发了 Controller=@Model.ControllerName

Action=@Model.ActionName

Message=@Model.Exception.Message

@model System.Web.Mvc.HandleErrorInfo 
<div class="container">

    <div class="row">
        <div class="col-md-6 col-md-offset-3">
            <div>
                <br />
                <div class="form-group">
                    <div class="row">
                        <div class="col-md-12">
                            <img src="~/Imagenes/logo.png" class="img-responsive center-block" />
                        </div>
                    </div>
                    <h2>Ooops an error has been triggered</h2>
                    <p>Controller = @Model.ControllerName</p>
                    <p>Action = @Model.ActionName</p>
                    <p>Message = @Model.Exception.Message</p>
                </div>
                @*<hr />*@
                <br />


                <div class="form-group">
                    <div class="col-md-12">
                        <a href="@Url.Action("TipoEvento", "Home")" class="pull-right linkedin-link">Regresar <i class="fa fa-angle-right"></i></a>
                    </div>
                </div>

            </div>

        </div>
    </div>
</div>
@*
*@
这就是抛出的错误


但是我真的需要显示这些信息(同样,出于开发目的),因此,请您帮助我并告诉我如何在错误页面中显示详细的错误信息,我将编写一个自定义错误处理程序属性并全局应用它。我写了一篇文章专门捕获授权异常并将其发送到特定页面。主要是从ExceptionContext获取操作和控制器信息

public class HandleUnauthorizedAttribute : HandleErrorAttribute
{
    public override void OnException(ExceptionContext filterContext)
    {
        base.OnException(filterContext);

        //remove the following line to capture all exceptions.  this only lets Security exceptions through
        if (filterContext.Exception.GetType() != typeof(SecurityException)) return;

        var controllerName = (string)filterContext.RouteData.Values["controller"];
        var actionName = (string)filterContext.RouteData.Values["action"];
        var model = new HandleErrorInfo(filterContext.Exception, controllerName, actionName);

        filterContext.Result = new ViewResult
        {
            //name your view whatever you want and place a matching view in /Views/Shared
            ViewName = "Unauthorized",
            ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
            TempData = filterContext.Controller.TempData
        };
        filterContext.ExceptionHandled = true;
        filterContext.HttpContext.Response.Clear();
        filterContext.HttpContext.Response.StatusCode = 403;
        filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
    }
}

在视图/共享目录中创建一个与过滤器中的视图名匹配的视图。

您好,您似乎只需要在应用程序开始时添加如下条目:

   protected void Application_Start()
    {       
        AreaRegistration.RegisterAllAreas();

       //Here is the entry
        RegisterGlobalFilters(GlobalFilters.Filters);


        RegisterRoutes(RouteTable.Routes);

        ModelBinders.Binders.DefaultBinder = new DevExpress.Web.Mvc.DevExpressEditorsBinder();
    }
注意:在上面的代码中,模型是空的,这就是为什么会出现错误

这样,它将自动将错误模型发送到视图


Source/Usefullink:

可能重复的Use
尝试{}catch(异常示例){}
,然后重定向到页面。您可以使用
TempData
存储您的异常,或者如果您愿意,使用另一种存储方法是的,我已经修改了FilterConfig,但仍然不知道将您发布的代码粘贴到哪里,在我的情况下,我已经有一个控制器:ErrorPageController和一个操作:ErrorMessage,我应该在代码中的何处定义控制器/操作?该属性只是一个类。将其放入目录调用/属性中。您不需要ErrorPageController或操作。ActionFilter会将您发送到视图。您调试过吗?过滤器被击中了吗?