如何处理在asp.net 3.0应用程序在我CustomAutorize属性错误 [英] How to handle errors in my CustomAutorize attribute in asp.net 3.0 Application

查看:152
本文介绍了如何处理在asp.net 3.0应用程序在我CustomAutorize属性错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我工作的一个asp.net MVC 3.0应用程序。我使用的用我自己的CustomRoleProvider
并通过覆盖默认属性CustomErrorHandler。

I am working on an asp.net MVC 3.0 Application. I am using using my own CustomRoleProvider and CustomErrorHandler by overriding default attributes.

每一件事情是工作的罚款。但是,问题是异常处理。

Every thing is working fine. But ,the problem is with the exception handling.

在测试中的应用,测试人员给予无效的数据库连接进行测试。

While testing the application , tester has given invalid DB connection to test.

其结果是,自定义错误处理程序不渲染错误查看,取而代之的则是路由原路径

The result is , Custom Error Handler is not rendering Error View , instead it is routing the original path

有关例如:

我正在我的应用程序

Home/Index

这是第一次打自定义角色提供给应用程序获取角色

It is first hitting Custom Role Provider to fetch the roles for the application

一直以来,数据库连接是不正确的,它是提高异常无法连接

Since , the Db Connection is not correct , it is raising exception that "Not able to Connect"

现在,而不是路由到错误查看与此错误消息一起。它被路由到家庭控制器和索引操作。

Now , Instead of routing to Error View along with this error message. It is routing to Home Controller and Index action.

**The code for my Custom Error Handler is as Follows**



public class CustomHandleErrorAttribute : HandleErrorAttribute    // Error handler 
    {
        public override void OnException(ExceptionContext filterContext)
        {
            if (filterContext.ExceptionHandled || !filterContext.HttpContext.IsCustomErrorEnabled)
            {
                return;
            }
            if (new HttpException(null, filterContext.Exception).GetHttpCode() != 500)
            {
                return;
            }
            if (!ExceptionType.IsInstanceOfType(filterContext.Exception))
            {
                return;
            }

            // if the request is AJAX return JSON else view.
            if (filterContext.HttpContext.Request.Headers["X-Requested-With"] == "XMLHttpRequest")
            {
                filterContext.Result = AjaxError(filterContext.Exception.Message, filterContext);
            }
            else
            {
                filterContext.ExceptionHandled = true;
                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
                {
                    ViewName = View,
                    MasterName = Master,
                    ViewData = new ViewDataDictionary<HandleErrorInfo>(model),
                    TempData = filterContext.Controller.TempData
                };
            }

        }
        protected JsonResult AjaxError(string message, ExceptionContext filterContext)
        {
            if (String.IsNullOrEmpty(message))
                message = "Something went wrong while processing your request. Please refresh the page and try again.";
            filterContext.HttpContext.Response.StatusCode = (int)HttpStatusCode.InternalServerError;
            filterContext.HttpContext.Response.TrySkipIisCustomErrors = true;
            return new JsonResult { Data = new { ErrorMessage = message }, ContentEncoding = System.Text.Encoding.UTF8, JsonRequestBehavior = JsonRequestBehavior.AllowGet };
        }
    }

在上面的code,设立filterContext.Result后。它不是渲染错误查看预期。

In the above code , after setting up filterContext.Result . It is not rendering Error View as Expected.

请纠正/建议我,我要去的地方错了..

Please correct/suggest me, where i am going wrong..

更新:

公共类CustomRoleProvider:// RoleProvider自定义角色提供
    {
公共重写字符串[] GetRolesForUser(用户名字符串)
        {

public class CustomRoleProvider : RoleProvider // Custom role provider { public override string[] GetRolesForUser(string username) {

          // Fetching roles for user from database 
    }

//其他一些方法

// Some other Methods

}
这是法产生异常,因为它试图连接到错误的连接。

} This is method is generating exception , since it is trying to connect to wrong connection

Updated2:

1)我使用自定义错误处理程序为整个控制器。

1) I am using Custom Error Handler for the entire controller.

2)我需要捕获所有异常包括阿贾克斯错误

2) I need to catch all the exceptions including Ajax Errors

3)我已经包括了我的code自定义错误处理程序上面

3) I have included my code for Custom Error Handler Above

4)我也使用CustomRole提供商为整个控制器

4) I am also using CustomRole Provider for entire controller

5)在这里,我想生成异常,通过给错误的数据库连接

5) Here, I am trying to generate exception , by giving wrong database connection

6)我运行的网址:首页/索引

6) I am running the URL : Home/Index

7)临睡前thatr URL,这是因为我使用它作为一个属性击中角色提供类中的方法

7) Before going to thatr URL, it is hitting the methods in Role Provider class since i am using it as a attribute

8)因为,我已经给了错误的DB连接,它产生的异常

8) Since, i have gave wrong DB Connection , It is generating exception

9)然后,触发在自定义错误处理异常的方法

9) Then, it fires on exception method of Custom error handler

10)建立误差模型的错误观点。

10) Building the Error Model for the error view

11),但这里的问题。相反,错误的渲染视图,它会到主控制器指数法。

11) But, here is the problem. Instead of rendering Error View , it is going to index method of the Home Controller.

12)但是,我需要查看错误在这里呈现,因为它无法连接到数据库,并让角色。我想在这里停止网址首页/指数furthuer执行。

12) But, i need Error View to be rendered here, because it has failed to connect to database and getting roles . I want furthuer execution of URL Home/Index to be stopped here.

希望这澄清了problem..i正在运行英寸请随时问我furthuer细节/澄清

Hope this clarifies the problem..i am running in to. please feel free to ask me for furthuer details/Clarification

推荐答案

的HandleError 的设计是能够注册的多个过滤器(例如针对不同的例外)。一个过滤器只能处理一些特定的异常或错误的情况下,另一个未经处理过的情况下可以通过另一个的HandleError 处理。我认为,目前的两个的标准,你的 [CustomHandleError] 过滤器被应用。您可以设置订单属性,指定从-1(最高优先级)优先级的整数值的任何正整数值。整数值越大,较低的过滤器的优先级。您可以使用<一个href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.filterattribute.order%28v=vs.108%29.aspx\"相对=nofollow>订单参数为例(见这里),让您的过滤器之前的工作。您可以在<一个发现的顺序更加全面描述href=\"http://msdn.microsoft.com/en-us/library/system.web.mvc.handleerrorattribute%28v=vs.108%29.aspx#specifying_the_order_property\"相对=nofollow> MSDN文档。

HandleError is designed to be able to register multiple filters (for example for different exceptions). One filter can handle only some specific exceptions or error cases and another unhandle cases can be handled by another HandleError. I suppose that currently both standard and your [CustomHandleError] filter are applied. You can set the Order property to an integer value that specifies a priority from -1 (highest priority) to any positive integer value. The greater the integer value is, the lower the priority of the filter is. You can use Order parameter for example (see here) to make your filter working before. More full description of the order you can find in the MSDN documentation.

答案这一个并的例如文章提供使用的小例子订单的属性的HandleError

The answer, this one and the article for example provide small examples of usage Order property of HandleError.

这篇关于如何处理在asp.net 3.0应用程序在我CustomAutorize属性错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆