是否有任何与 AJAX 相关的属性要为 ASP.NET MVC 控制器操作设置? [英] Is there any attribute relating to AJAX to be set for ASP.NET MVC controller actions?
问题描述
我想在 ASP.NET MVC 中使用带有 AJAX 调用的部分视图,这是我第一次使用它.我只是搜索了一下,看看有没有什么特别的我应该事先知道的,其中一个我很好奇的是,看看是否有任何应该设置的特殊属性或与 AJAX 调用相关的属性?类似于 [ChildActionOnly]
或 [HttpGet]
I want to use partial views with AJAX calls in ASP.NET MVC, and this is the first time I'm using it. I just searched to see if there is anything special I should know beforehand, and one of'em that I'm curious about, is to see if there is any special attribute that should be set or is related to AJAX calls? Something like [ChildActionOnly]
or [HttpGet]
推荐答案
我认为 ajax 没有内置属性,但您可以像这样创建自己的 AjaxOnly
过滤器:>
I don't think there is built in attribute for ajax, but you can create your own AjaxOnly
filter like this:
public class AjaxOnlyAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
}
}
并像这样装饰你的操作方法:
And decorate your action methods like this:
[AjaxOnly]
public ActionResult AjaxMethod()
{
}
另见:ASP.NET MVC Action Filter – Ajax Only Attribute 用于另一种实现方式
See Also: ASP.NET MVC Action Filter – Ajax Only Attribute for another way of implementing this
这篇关于是否有任何与 AJAX 相关的属性要为 ASP.NET MVC 控制器操作设置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!