MVC5 Attribute(特性)
AuthorizeAttribute:一般用来判断权限
ActionFilterAttribute:方法执行前后动作
OutputCacheAttribute:输出缓存设置
注:我们创建名称的时候请带上 Attribute
AuthorizeAttribute:创建
namespace MyWebApi.Models
{
using System;
using System.Web;
using System.Web.Mvc; /// <summary>
/// 检查登陆已经权限问题
/// </summary>
public class MyAuthorizeAttribute : AuthorizeAttribute
{
public int status = ;//1:登陆有权,2:没有登入,3:登陆无权限
/// <summary>
/// 权限检查的入口
/// </summary>
/// <param name="httpContext">http的基类</param>
/// <returns></returns>
protected override bool AuthorizeCore(HttpContextBase httpContext)
{
//这里写逻辑 利用Cookies 判断用户是否登入已经登入是否有权限,然后来给 status 赋值
#region 我们也可以在使用的时候传入指定的用户以及角色来判断
string[] users = Users.Split(',');
string[] roles = Roles.Split(',');
#endregion
return status ==;
} /// <summary>
/// 当检查入口返回false会进入到这里
/// </summary>
/// <param name="filterContext"></param>
protected override void HandleUnauthorizedRequest(AuthorizationContext filterContext)
{
if (filterContext == null)
{
throw new ArgumentNullException("filterContext");
}
else
{
if (status == ) //没有登陆的页面
{
filterContext.Result = new RedirectResult("");
}
else if (status == ) //没有权限访问的页面
{
filterContext.Result = new RedirectResult("");
}
}
}
}
}
使用:

ActionFilterAttribute:创建
namespace MyWebApi.Models
{
using System.Web.Mvc; /// <summary>
/// 方法的过滤
/// </summary>
public class MyFilterAttribute:ActionFilterAttribute
{
/// <summary>
/// 在方法执行之前
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuting(ActionExecutingContext filterContext)
{
//逻辑操作
filterContext.Result = new RedirectResult("");//跳转页面
}
/// <summary>
/// 在方法之后返回之前
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
//逻辑操作
filterContext.Result = new RedirectResult("");//跳转页面
}
/// <summary>
/// 在返回之前
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
//逻辑操作
filterContext.Result = new RedirectResult("");//跳转页面
}
/// <summary>
/// 在返回之后
/// </summary>
/// <param name="filterContext"></param>
public override void OnResultExecuted(ResultExecutedContext filterContext)
{
//逻辑操作
filterContext.Result = new RedirectResult("");//跳转页面
}
}
}
使用:

OutputCacheAttribute:输出缓存不必我们去创建,System.Web.Mvc 命名空间中已存在,我们可以直接使用,在设置的时间内刷新页面不会刷新内容

特性上设置参数:

web.config配置:


MVC5 Attribute(特性)的更多相关文章
- mvc5新特性RouteAttribute特征路由
mvc5新特性RouteAttribute特征路由,本文讲述如何开启mvc5的RouteAttribute路由功能并附上一个实例说明RouteAttribute是怎么工作的 mvc5新特性RouteA ...
- .net学习之Attribute特性和EF关键知识点
一.Attribute特性/标签1.Attribute用来对类.属性.方法等标注额外的信息,贴一个标签简单的说,定制特性Attribute,本质上就是一个类,它为目标元素提供关联附加信息,并在运行时以 ...
- [C#]Attribute特性(3)——AttributeUsage特性和特性标识符
相关文章 [C#]Attribute特性 [C#]Attribute特性(2)——方法的特性及特性参数 AttributeUsage特性 除了可以定制自己的特性来注释常用的C#类型外,您可以用At ...
- [C#]Attribute特性(2)——方法的特性及特性参数
上篇博文[C#]Attribute特性介绍了特性的定义,类的特性,字段的特性,这篇博文将介绍方法的特性及特性参数相关概念. 3.方法的特性 之所以将这部分单列出来进行讨论,是因为对方法的特性查询的反射 ...
- 关于C# 中的Attribute 特性
关于C# 中的Attribute 特性 作者: 钢钢 来源: 博客园 发布时间: 2011-01-09 23:30 阅读: 13921 次 推荐: 12 原文链接 [收藏] 摘要:纠结地说 ...
- Attribute特性验证模型model
数据验证我们往往分为前台验证和后台验证,而我们的后台验证每到一个方法中就要去验证一次,这样的代码想想都难以维护,这篇我们这篇文章就是为了解决这样的问题.用attribute 这个特性来解决这样的问题 ...
- MVC5+ 路由特性
MVC5+ 路由特性 概述 ASP.NET MVC 5支持一种新的路由协议,称为路由特性. MVC5也支持以前定义路由的方式,你可以在一个项目中混合使用这两种方式来定义路由. 案例 1.使用Visua ...
- 如何在方法上贴上attribute(特性)捕捉方法的异常,来实现我们的需求
在方法上贴上attribute(特性)捕捉方法的异常,其实这么做也是为了在项目中不会大量使用try-cacth这样的语句,同时使我们的代码看起来更简洁,更直观,将逻辑业务分离使得后期维护方便.这里我们 ...
- .NET进阶篇03-Reflection反射、Attribute特性
知识需要不断积累.总结和沉淀,思考和写作是成长的催化剂 内容目录 一.概述二.反射1.反射使用2.创建对象3.调用方法4.字段属性三.特性四.总结 一.概述 反射其实无处不在,我们用VS进行调试时候, ...
- Net中Attribute特性的高级使用及自定义验证实现
好久没写博客了,今天在百忙之中抽空来写篇文章,记录一下最近深入学习Attribute特性的笔记及心得.~~ 一.什么是特性? 特性(Attribute)是用于在运行时传递程序中各种元素(比如类.方法. ...
随机推荐
- PCU
PCU(Peak concurrent users ),互联网术语,应用在网络游戏和其他互联网服务领域,意思是最高同时在线人数 业务系统架构性能提升主要分为两种不同的方式,scale-out(横向扩展 ...
- 【转】sqlserver字符串拆分(split)方法汇总
Java..net等开发工具具有split功能,最近在Sqlserver中碰到这个需求. 方法1:动态SQL法 ),) set @string='1,2,3,4,5,6,7,8,9,10' set @ ...
- OLEDB 简单数据查找定位和错误处理
在数据库查询中,我们主要使用的SQL语句,但是之前也说过,SQL语句需要经历解释执行的步骤,这样就会拖慢程序的运行速度,针对一些具体的简单查询,比如根据用户ID从用户表中查询用户具体信息,像这样的简单 ...
- oracle相关常识
1.数据类型 VARCHAR2() NUMBER() DATE CLOB BLOB 2.复制表:create table tableName as select * from emp3.新增列:ALT ...
- 关于Android那些事
1.开发Activity步骤 第一步:写一个类继承Activity 第二步:重写oncreate方法 第三步:在主配置文件中注册activity <activity android:name=& ...
- Format - DateTime
1. Long Date/Short Date/Long Time/Short Time,可以在系统的“Region and Language”中找到相应设置: 2. ISO Format/Local ...
- PhoneGap API介绍:Events
事件类型: backbutton deviceready menubutton pause resume searchbutton online offline backbutton 当用户在Andr ...
- vscode 显示"没有活动的源代码控制提供程序“处理
不知为何我的 VS Code 在 1.25 版本开始就一直 ”没有活动的源代码控制提供程序“,找了好几天都没找到,今天终于找到怎么处理了, 切换到插件中找到下图对应的 Git (可以直接再上面搜索框输 ...
- MySQL中AddDate函数的疑惑
无论使用哪一种RDBMS,都需要使用到其中的一些日期转换函数,在使用MySQL的AddDate函数时,遇到了点小问题,稍作记录. root@localhost:mysql3376.sock [(non ...
- Centos7设置文件夹写入权限
用 root 账号执行chmod命令: #chmod -R 777 dirPath 参数 -R 表示递归,dirPath及其之内的所有文件夹.文件都被改变了权限. 例子: #chmod -R 777 ...