C#MVC中Controler的自定义属性使用
2016-06-16 21:48
204 查看
1、首先声明自定义属性
例如:
public class NeedPassAttribute:Attribute
{
/// <summary>
/// 需要登录(导航到小登录页)
/// </summary>
public bool NeedLogin { get; set; }
}
2、在Controler的基类继承类中重载OnActionExecuting(ActionExecutingContext filterContext)方法中判断并处理
例如:var attrNeeds = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NeedPassAttribute), false);
if (attrNeeds.Length == 1)
{
NeedPassAttribute needPass = attrNeeds[0] as NeedPassAttribute;
if (needPass.NeedLogin)
{ 处理代码}
}
例如:
public class NeedPassAttribute:Attribute
{
/// <summary>
/// 需要登录(导航到小登录页)
/// </summary>
public bool NeedLogin { get; set; }
}
2、在Controler的基类继承类中重载OnActionExecuting(ActionExecutingContext filterContext)方法中判断并处理
例如:var attrNeeds = filterContext.ActionDescriptor.GetCustomAttributes(typeof(NeedPassAttribute), false);
if (attrNeeds.Length == 1)
{
NeedPassAttribute needPass = attrNeeds[0] as NeedPassAttribute;
if (needPass.NeedLogin)
{ 处理代码}
}
相关文章推荐
- 练习、C# 结构体、冒泡排序
- C#中的代理
- C# 生成随机索引列表
- 关于C#对图片部分操作(水印、透明度)
- U3D结合第三方库ICSharpCode.SharpZipLib 实现文件夹打包解包
- C# UDP收发请求工具类
- C# 多线程是否结束可通过线程池可以判断
- VS2015中C#版本6.0的新特性 你需要知道
- C#中lambda表达式浅析
- C#中数组、ArrayList和List三者的区别
- [置顶] C# 如何利用反射,将字符串转化为类名并调用类中方法
- 自定义热键的代码(C#)
- C#套接字Socket编程之最简单通信
- C# DateTime 格式化 奇怪问题!
- C# TransactionScope 使用
- C#导出Excel
- C#复习④
- C# 设置鼠标指针
- C#虚基类继承与接口的区别
- C#中实体集合和实体转换成相应的string、XDocument、XElement