ASP.NET MVC ActionMethodSelectorAttribute 以及HttpGet等Action特性
一、ActionMethodSelectorAttribute
其是一个抽象类,继承自Attribute,子类有NonActionAttribute、HttpGetAttribute、HttpPostAttribute、HttpPutAttribute、HttpDeleteAttribute、HttpPatchAttribute、HttpHeadAttribute、HttpOptionsAttribute和AcceptVerbsAttribute,其唯一抽象方法IsValidForRequest,如果返回false,结果会提示Action Not Found
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public abstract class ActionMethodSelectorAttribute : Attribute
{
public abstract bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo);
}
AcceptVerbsAttribute 直接继承 ActionMethodSelectorAttribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class AcceptVerbsAttribute : ActionMethodSelectorAttribute
{
public AcceptVerbsAttribute(HttpVerbs verbs)
: this(EnumToArray(verbs))
{
} public AcceptVerbsAttribute(params string[] verbs)
{
if (verbs == null || verbs.Length == )
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "verbs");
} Verbs = new ReadOnlyCollection<string>(verbs);
} public ICollection<string> Verbs { get; private set; } private static void AddEntryToList(HttpVerbs verbs, HttpVerbs match, List<string> verbList, string entryText)
{
if ((verbs & match) != )
{
verbList.Add(entryText);
}
} internal static string[] EnumToArray(HttpVerbs verbs)
{
List<string> verbList = new List<string>(); AddEntryToList(verbs, HttpVerbs.Get, verbList, "GET");
AddEntryToList(verbs, HttpVerbs.Post, verbList, "POST");
AddEntryToList(verbs, HttpVerbs.Put, verbList, "PUT");
AddEntryToList(verbs, HttpVerbs.Delete, verbList, "DELETE");
AddEntryToList(verbs, HttpVerbs.Head, verbList, "HEAD");
AddEntryToList(verbs, HttpVerbs.Patch, verbList, "PATCH");
AddEntryToList(verbs, HttpVerbs.Options, verbList, "OPTIONS"); return verbList.ToArray();
} public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
if (controllerContext == null)
{
throw new ArgumentNullException("controllerContext");
} string incomingVerb = controllerContext.HttpContext.Request.GetHttpMethodOverride(); return Verbs.Contains(incomingVerb, StringComparer.OrdinalIgnoreCase);
}
}
除了NonActionAttribute,内部都是通过AcceptVerbsAttribute 来实现的,如HttpGetAttribute,其他都类似
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class HttpGetAttribute : ActionMethodSelectorAttribute
{
private static readonly AcceptVerbsAttribute _innerAttribute = new AcceptVerbsAttribute(HttpVerbs.Get); public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return _innerAttribute.IsValidForRequest(controllerContext, methodInfo);
}
}
NonActionAttribute,IsValidForRequest直接返回false
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class NonActionAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo)
{
return false;
}
}
二、ActionNameSelectorAttribute
其是一个抽象类,继承自Attribute,子类有ActionNameAttribute
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public abstract class ActionNameSelectorAttribute : Attribute
{
public abstract bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo);
}
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false, Inherited = true)]
public sealed class ActionNameAttribute : ActionNameSelectorAttribute
{
public ActionNameAttribute(string name)
{
if (String.IsNullOrEmpty(name))
{
throw new ArgumentException(MvcResources.Common_NullOrEmpty, "name");
} Name = name;
} public string Name { get; private set; } public override bool IsValidName(ControllerContext controllerContext, string actionName, MethodInfo methodInfo)
{
//只是验证根据请求进行路由匹配出的actionName,是否和ActionName特性上指定的Name相等
return String.Equals(actionName, Name, StringComparison.OrdinalIgnoreCase);
}
}
三、自定义ActionMethodSelectorAttribute
验证请求是GET而且是ajax的
public class MyActionMethodSelectorAttribute : ActionMethodSelectorAttribute
{
public override bool IsValidForRequest(ControllerContext controllerContext, System.Reflection.MethodInfo methodInfo)
{
//get/post
string httpMethodOverride = controllerContext.HttpContext.Request.GetHttpMethodOverride(); //isAjax
var isAjax = controllerContext.HttpContext.Request.IsAjaxRequest(); var b = httpMethodOverride.ToLower() == "get" && isAjax; return b;
}
}
ASP.NET MVC ActionMethodSelectorAttribute 以及HttpGet等Action特性的更多相关文章
- ASP.NET MVC 5使用Filter过滤Action参数防止sql注入,让你代码安全简洁
在开发程序的过程中,稍微不注意就会隐含有sql注入的危险.今天我就来说下,ASP.NET mvc 5使用Filter过滤Action参数防止sql注入,让你代码安全简洁.不用每下地方对参数的值都进行检 ...
- [转]ASP.NET MVC中的两个Action之间值的传递--TempData
本文转自:ASP.NET MVC中的两个Action之间值的传递--TempData 一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在 ...
- [asp.net mvc 奇淫巧技] 03 - 枚举特性扩展解决枚举命名问题和支持HtmlHelper
一.需求 我们在开发中经常会遇到一些枚举,而且这些枚举类型可能会在表单中的下拉中,或者单选按钮中会用到等. 这样用是没问题的,但是用过的人都知道一个问题,就是枚举的命名问题,当然有很多人枚举直接中文命 ...
- ASP.NET MVC中的两个Action之间值的传递--TempData
一. ASP.NET MVC中的TempData 在ASP.NET MVC框架的ControllerBase中存在一个叫做TempData的Property,它的类型为TempDataDictiona ...
- ASP.NET mvc下在Controller下action的跳转方式
在ASP.NET mvc下,action有多种挑战方式: return RedirectToAction("Index");//一个参数时在本Controller下 如果Redir ...
- Asp.Net MVC<六>:Controller、Action 待续
控制器 抽象类Controller Visual Studio的向导创建的Controller类型继承自抽象类Controller. 它是ControllerBase的子类. 实现了IControll ...
- ASP.NET MVC什么时候使用异步Action
在没有使用异步Action之前,在Action内,比如有如下的写法: public ActionResult Index() CustomerHelper cHelper = new Customer ...
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法(mvc部分视图的添加)
Partial 和RenderPartial:这两个的性质都是一样, 只指把一个个View给镶入进来, 只是回传值有点不一样Partial 回传的一个Object (MvcHtmlString), 回 ...
- Asp.net MVC中Html.Partial, RenderPartial, Action,RenderAction 区别和用法
Partial 和RenderPartial:这两个的性质都是一样, 只指把一个个View给镶入进来, 只是回传值有点不一样Partial 回传的一个Object (MvcHtmlString), 回 ...
随机推荐
- How to update XENTRY Connect C5 software with .iso file
07.2018 Xentry Mercedes SD Connect c5 software update manual for newbies: Important: If you have XDO ...
- 弹出DIV锁定代码
<html> <head> <meta http-equiv="Content-Type" content="text/html; ch ...
- 栈(NOIP2003&水题测试2017082501)
题目链接:栈 这题不难. 我们看一下,其实可以发现是卡特兰数. 不知道卡特兰数?没事,给你简单讲一下. 卡特兰数的递推式f(n)=f(0)*f(n-1)+f(1)*f(n-2)+-+f(n-2)*f( ...
- 449. Serialize and Deserialize BST
https://leetcode.com/problems/serialize-and-deserialize-bst/#/description Serialization is the proce ...
- 2019.01.26 codeforces 632E. Thief in a Shop(生成函数)
传送门 题意简述:给nnn个物件,物件iii有一个权值aia_iai,可以选任意多个.现在要求选出kkk个物件出来(允许重复)问最后得到的权值和的种类数. n,k,ai≤1000n,k,a_i\le ...
- 2018.01.04 bzoj5291: [Bjoi2018]链上二次求和(线段树)
传送门 线段树基础题. 题意:给出一个序列,要求支持区间加,查询序列中所有满足区间长度在[L,R][L,R][L,R]之间的区间的权值之和(区间的权值即区间内所有数的和). 想题555分钟,写题202 ...
- 2018.12.22 spoj7258 Lexicographical Substring Search(后缀自动机)
传送门 samsamsam基础题. 题意简述:给出一个串,询问第kkk大的本质不同的串. 然而这就是弦论的简化版. 我们把samsamsam建出来然后贪心选择就行了. 代码: #include< ...
- 2018.11.01 NOIP训练 树的排列(树形dp)
传送门 跟这道题差不多. 只不过是让权值小的儿子做权值大的儿子的父亲而已. 代码
- R入门(一)
简单的算术操作和向量运算 向量赋值:函数c( ),参数可以是一个或多个数,也可以是向量 赋值符号‘<-’ 向量运算:exp(),log(),sin(),tan(),sqrt(),max(),mi ...
- Educational Codeforces Round 54 E. Vasya and a Tree(树上差分数组)
https://codeforces.com/contest/1076/problem/E 题意 给一棵树(n<=3e5),m(3e5)次查询,每次查询u,d,x,表示在u的子树中,给距离u&l ...