ASP.NET MVC ActionFilter - 确定是否AJAX请求 [英] ASP.NET MVC ActionFilter - Determine if AJAX Request

查看:151
本文介绍了ASP.NET MVC ActionFilter - 确定是否AJAX请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是ActionFilter来确定用户是否具有访问特定的资源,如击球动作之前Account对象(一拉犀牛安全性)。这是一个全球性的过滤器会被重定向到一个错误页面应在授权值失败

I am using an ActionFilter to determine if a user has access to a specific resource such as an Account object (a la Rhino Security) before hitting an action. This is a global filter which redirects to an error page should the authorization value fail

我用下面的code,工作正常全页面请求:

I'm using the following code, which works fine for full page requests:

filterContext.Controller.TempData["ErrorMessage"] = string.Format("You are not authorized to perform operation: {0}", operation);
filterContext.Result = new RedirectResult("~/Error/AuthorizationError");

Ajax请求,我不希​​望应用重定向,而是返回一个错误信息。有没有办法告诉动作过滤器内,如果这是一个Ajax请求或定期整页(抱歉不知道正确的术语)的要求?

Ajax requests I do not want to apply a redirect, but rather return an error message. Is there a way to tell inside the action filter if this is an AJAX request or a regular full page (sorry not sure of the correct terminology) request?

在此先感谢

JP

推荐答案

您可以使用IsAjaxRequest扩展方法:

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");
}

这篇关于ASP.NET MVC ActionFilter - 确定是否AJAX请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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