我使用 ActionFilter 来确定用户在执行操作之前是否有权访问特定资源,例如帐户对象(la Rhino Security)。这是一个全局过滤器,如果授权值失败,它会重定向到错误页面
我正在使用以下代码,它适用于整页请求:
filterContext.Controller.TempData["ErrorMessage"] = string.Format("You are not authorized to perform operation: {0}", operation);
filterContext.Result = new RedirectResult("~/Error/AuthorizationError");
Ajax 请求我不想应用重定向,而是返回错误消息。有没有办法在操作过滤器内部判断这是 AJAX 请求还是常规整页(抱歉不确定正确的术语)请求?
提前致谢
JP
你可以使用是Ajax请求 http://msdn.microsoft.com/en-us/library/system.web.mvc.ajaxrequestextensions.isajaxrequest.aspx扩展方法:
if (filterContext.HttpContext.Request.IsAjaxRequest())
{
// it was an AJAX request
...
}
else
{
// it was a standard request
filterContext.Controller.TempData["ErrorMessage"] = string.Format("You are not authorized to perform operation: {0}", operation);
filterContext.Result = new RedirectResult("~/Error/AuthorizationError");
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)