您的位置:首页 > 编程语言 > ASP

Asp.net Mvc Preview 5 体验--实现ActionSelectionAttribute来判断是否为AJAX请求而选择不同的Action

2008-08-30 19:13 731 查看
ActionSelectionAttribute是ASP.NET MVC Preview 5 提供的一个抽象基类,通过ActionSelectionAttribute的命名我们就可以猜想到这个Attribute是用来选择(匹配)Action方法的。该抽象类只提供了一个抽象的方法 IsValidForRequest,该方法会在Controller的ActionInvoker被调用。如果一个Action加上了该Attribute,那么只有当IsValidForRequest方法返回true的时候,当前的请求才会匹配该Action。

public class GetByAjaxAttribute : ActionSelectionAttribute

public class GetByAjaxAttribute : ActionSelectionAttribute

[ActionName("Add"), AcceptVerbs("GET")]

public ActionResult AddByGet()

[ActionName("Add"), AcceptVerbs("POST"), GetByAjax(true)]

public ActionResult AddByAjax()

//这里要加上GetByAjax(false)哦,

//否则当POST过来的时候,这个也是符合条件的,

//则会匹配两个Action(这个和上面一个),就抛出异常了

[ActionName("Add"), AcceptVerbs("POST"), GetByAjax(false)]

public ActionResult AddByPost()

{

//Do Your Thing here

ViewData["Message"] = "通过POST方式访问";

return View();

}

最后是该文章的的示例代码: ActionSelectionAttributeDemo.rar
Enjoy!

参考文章:How a Method Becomes An Action
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: