【问题标题】:Exception handling with Elmah installed in an ASP.NET MVC 4 Application使用安装在 ASP.NET MVC 4 应用程序中的 Elmah 进行异常处理
【发布时间】:2013-01-15 12:32:42
【问题描述】:

我已经建立了一个很好的 MVC4 应用程序,决定保护它,一切都很好。以下是我用来帮助防止 XSS 攻击的过滤器。

public class IsPostedFromThisSiteAttribute : AuthorizeAttribute
{
    public override void OnAuthorization(AuthorizationContext filterContext)
    {
        //TODO: - Possible problems in future:
        //1. Is there a way to handle the execptions and just display a friendly error page / message.

        if (filterContext.HttpContext != null)
        {
            if (filterContext.HttpContext.Request.UrlReferrer == null)
                throw new System.Web.HttpException("Invalid Submission");

            //if (filterContext.HttpContext.Request.UrlReferrer.Host != "localhost/YeatsClinical.PatientPortal.Web")
            if (filterContext.HttpContext.Request.UrlReferrer.AbsolutePath != ConfigurationManager.AppSettings["SiteURL"])
                throw new System.Web.HttpException("This form was not submitted from this site!");
        }
    }
}

我的 MVC 应用安装了 Elmah,它也很好用。

问题是,我知道上面的错误,我不想显示异常消息,或者记录它或类似的东西(所有这些我都可以轻松完成)。相反,我想通过向我的用户显示一条漂亮的小消息来正确处理它。如何更改上面的代码以允许它显示带有漂亮小消息的视图或部分视图,或者只是在某处输出一些文本以让用户以一种不会导致应用程序崩溃的良好方式知道出了什么问题,也许在这种特殊情况下,将他返回到正确的登录屏幕。

【问题讨论】:

    标签: c# asp.net-mvc asp.net-mvc-4 error-handling elmah


    【解决方案1】:

    只需在filterContext 上分配Result 属性:

    public class IsPostedFromThisSiteAttribute : AuthorizeAttribute
    {
        public override void OnAuthorization(AuthorizationContext filterContext)
        {
            //TODO: - Possible problems in future:
            //1. Is there a way to handle the execptions and just display a friendly error page / message.
    
            if (filterContext.HttpContext != null)
            {
                if (filterContext.HttpContext.Request.UrlReferrer == null)
                {
                    var viewResult = new ViewResult
                    {
                        ViewName = "~/Views/Shared/Invalid.cshtml"
                    };
                    filterContext.Result = viewResult;
                    return;
                }
    
                if (filterContext.HttpContext.Request.UrlReferrer.AbsolutePath != ConfigurationManager.AppSettings["SiteURL"])
                {
                    var viewResult = new ViewResult
                    {
                        ViewName = "~/Views/Shared/InvalidSite.cshtml"
                    };
                    filterContext.Result = viewResult;
                    return;
                }
            }
        }
    }
    

    这将阻止执行控制器操作并立即执行您分配给filterContextResult 属性的ActionResult。基本上,您正在使操作的执行短路。你可以返回任何你想要的 ActionResult:ViewResult、PartialViewResult、JsonResult、RedirectToRouteResult、...

    【讨论】:

    • 感谢您的回答。我收到一个错误,但它与此代码无关,这与我的登录页面有关,我收到一个不属于问题的错误。我制作了一个示例项目并使用您的代码来验证它是否有效。因此,我会接受您的回答,并再次感谢您对此的帮助。
    猜你喜欢
    • 2011-03-24
    • 1970-01-01
    • 2015-07-21
    • 2011-12-05
    • 2013-08-13
    • 2010-11-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多