asp.net mvc 自定义异常过滤器强制返回完整视图,而不是部分视图
我有一个自定义异常过滤器,我通过向我的类添加 [CustomExceptionFilter] 属性来调用它。它按照我希望的方式工作,但是如果操作方法返回部分视图(通过 ajax 请求),则异常(基本上是重定向到未经授权的页面)正在加载部分视图页。有没有办法强制它重新加载“父”网址?
这是自定义异常过滤器的代码
public class CustomExceptionFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception.GetType() == typeof(CustomSecurityException))
{
filterContext.ExceptionHandled = true;
RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "NoAccess", action = "Index", message = filterContext.Exception.Message })).VirtualPath;
filterContext.HttpContext.Response.Redirect(url, true);
}
}
}
I have a custom exception filter that I'm calling by virtue of adding a [CustomExceptionFilter] attribute to my class. It works as I'd like it to, however if the action method is returning a partial view (through an ajax request), the exception (which is basically a redirect to a not authorized page), is loading up the partial view with that page. Is there a way I can force it to reload the 'parent' url?
Here is the code for the custom exception filter
public class CustomExceptionFilter : FilterAttribute, IExceptionFilter
{
public void OnException(ExceptionContext filterContext)
{
if (filterContext.Exception.GetType() == typeof(CustomSecurityException))
{
filterContext.ExceptionHandled = true;
RequestContext rc = new RequestContext(filterContext.HttpContext, filterContext.RouteData);
string url = RouteTable.Routes.GetVirtualPath(rc, new RouteValueDictionary(new { Controller = "NoAccess", action = "Index", message = filterContext.Exception.Message })).VirtualPath;
filterContext.HttpContext.Response.Redirect(url, true);
}
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
这是您需要在浏览器上处理的事情。例如,尝试处理 error() on jQuery.ajax() 调用(显然不不返回重定向..)。
This is something you need to handle on the browser. Try handling the error() on jQuery.ajax() call for example (and obviously don't return redirect..).
我建议让异常冒泡给客户端并像麦克斯韦建议的那样处理它。
在我们之前的项目中,我们使用了特定的 actionfilter 来处理 ajax 错误(借自 Suteki Shop) 。请注意,响应状态为 500(内部服务器错误)。为了在 JQuery.ajax() 调用中调用 de Error() 委托,响应需要错误状态。
I would suggest letting the exception bubble up to the client and handle it like Maxwell suggested.
In our previous project we used a specific actionfilter for handling ajax errors (borrowed from Suteki Shop). Note that the response status is 500 (internal server error). An error status is required for the response in order to call de Error() delegate within a JQuery.ajax() call.
我在表单标签中使用 OnFailure 处理程序。
...
函数 ajaxFormSucced(){
// 成功代码
}
函数 ajaxFormFailure(){
// 失败代码
}
I use the OnFailure hanlder in form tag.
...
function ajaxFormSucced(){
// Code for success
}
function ajaxFormFailure(){
// Code for failure
}
您可以验证该请求是否是 ajax 请求。
例如,您可以执行以下操作...
但是,正如 Maxwell 所说,如果它是 ajax 请求,您可以让异常冒泡,并在客户端处理错误。您可以全局设置一种处理 ajax 请求中异常的方法,如此处所述
You can verify if the request is an ajax request or not.
You could for example do the following...
However, as Maxwell said you could let the exeption bubble up if it is an ajax request and handle the error on the client. You can setup globally a way of handling exceptions in ajax requests like it is described here
您是否尝试清除响应?控制器可能仍在设置响应内容。
Did you try clearing the response? The controller may still be setting response content.
这个链接对我有帮助
使用 jQuery 通过 Ajax 发布到控制器时处理 ASP.NET MVC 异常
最后,测试 javascript 时函数,从第一行的alert开始。函数中的任何 JavaScript 错误都可能会在执行过程中停止函数,而不会通过浏览器反馈 JavaScript 错误(取决于您的设置)。
This link helped me
Handling ASP.NET MVC exceptions when posting to the controller via Ajax using jQuery
Lastly, When testing the javascript function, start with alert on the first line. Any javascript errors in your function are likely to stop the function in mid execution without javascript error feedback via the browser (depending on your setup).
这会对您有所帮助。
只需添加 .IsAjaxRequest 扩展方法并向浏览器返回 403 状态代码,jquery ajaxError 将处理它重定向到登录页面
This will help you.
Just add .IsAjaxRequest extension method and return 403 status code to browser, jquery ajaxError will handle it redirecting to login page
正如 Maxwell 所说,在客户端上处理,使用类似这样的东西。
您必须做的是确保在控制器中 NoAccess 的 ActionResult 代码包含以下代码,以便触发您的 ajax 错误。
As Maxwell says, handle on the client, using something like this
What you must do though is make sure in the controller ActionResult code for NoAccess contains the following code, so that your ajax error is triggered.