是否有关于AJAX的ASP.NET MVC控制器动作被设置任何属性? [英] Is there any attribute relating to AJAX to be set for ASP.NET MVC controller actions?

查看:211
本文介绍了是否有关于AJAX的ASP.NET MVC控制器动作被设置任何属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用部分意见与AJAX在ASP.NET MVC中称,这是我第一次使用它。我只是搜查,看看是否有什么特别的,我应该事先知道,一个of'em,我很好奇,就是看是否有应设置或涉及到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]

推荐答案

我不认为有内置的属性,阿贾克斯,但它不是太难创建一个。你可以创建一个像

I don't think there is built in attribute for ajax but its not too hard to create one. you can create AjaxOnly filter like

public class AjaxOnlyAttribute : ActionMethodSelectorAttribute 
    {
        public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
        {
            return controllerContext.RequestContext.HttpContext.Request.IsAjaxRequest();
        }
    }

和装饰像

[AjaxOnly]
public ActionResult ajaxMethod()
{

}

这个帖子查找的另一种方式实施这一属性。

Look at this post for another way of implementing this attribute

这篇关于是否有关于AJAX的ASP.NET MVC控制器动作被设置任何属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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