我希望能够在控制器上标记一个动作,以便通过ajax调用和通过RenderAction调用。问题是这两个属性都派生或实现了不同的抽象。下一步是出路:将AjaxOnlyAttribute和ChildActionOnlyAttribute合并为一个动作过滤器
[AjaxOnly]
PartialViewResult GetViewAjax(int foo) { return GetView(foo); }
[ChildActionOnly]
PartialViewResult GetView(int foo) { ... }
但是,这并不是完整的。
的AjaxOnly属性,我说的是:
public sealed class AjaxOnlyAttribute : ActionFilterAttribute
{
#region Public members
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
if (filterContext == null)
throw new ArgumentNullException("filterContext");
if (filterContext.HttpContext.Request.Headers["X-Requested-With"] != "XMLHttpRequest")
filterContext.Result = new HttpNotFoundResult();
}
#endregion
}
这种方法是从MVC3期货拍摄。为什么条件不filterContext.HttpContext.Request.IsAjaxRequest()
是由开发团队制作,并表示下一个重要的注释:
// Dev10 #939671 - If this attribute is going to say AJAX *only*, then we need to check the header
// specifically, as otherwise clients can modify the form or query string to contain the name/value
// pair we're looking for.
为什么?假设您的动作通过RenderAction()和ajax请求从不同的地方重新使用。假设它们在不同的页面上被重复使用,就像在许多地方绘制一些标准的工具面板一样,并且在不同的环境下使用略有不同 – 2012-03-02 13:50:43
@Hohhi,所以你希望你的行为只能通过AJAX调用或者作为Child action ? – 2012-03-02 14:00:04
的确如此,感谢您的建议,我会更新问题并告诉我在一段时间内有多远 – 2012-03-02 14:06:49