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)是用于在运行时传递程序中各种元素(比如类.方法. ...
随机推荐
- 8086实时时钟实验(二)——《x86汇编语言:从实模式到保护模式》读书笔记06
上次我们说了代码,这次我们说说怎样看到实验结果. 首先编译源文件(我的源文件就在当前路径下,a盘和c盘在上一级目录下): nasm -f bin c08_mbr.asm -o c08_mbr.bin ...
- What is Network Address Translation?---reference
http://whatismyipaddress.com/nat What is Network Address Translation? Network Address Translation (N ...
- bzoj 2741: 【FOTILE模拟赛】L
Description FOTILE得到了一个长为N的序列A,为了拯救地球,他希望知道某些区间内的最大的连续XOR和. 即对于一个询问,你需要求出max(Ai xor Ai+1 xor Ai+2 .. ...
- select操作大全
每一次操作select的时候,总是要出来翻一下资料,不如自己总结一下,以后就翻这里了. 比如<select class="selector"></select&g ...
- SQL更新派工单数量=任务数量的
select b.FCommitQty '任务数量',a.FQty '派工数量',a.FSourceBillNo '派工单号',b.FBillNo '任务单号',a.FStatus '派工状态' fr ...
- git把dev部分提交过的内容合并到master
git 把dev部分提交过的内容合并到master $ git reflog a6de5cc HEAD@{}: checkout: moving from wf_dev to master 303aa ...
- 亲测,很有效的忽略SSL证书方法
1.在httpclient发起请求时,有时会出现下面这种情况 你的日志中出现有关SSL的异常,javax.net.ssl.SSLPeerUnverifiedException: peer not au ...
- Eclipse equinox implementation of OSGi
Bundle package org.osgi.framework; public interface Bundle extends Comparable<Bundle> { int UN ...
- Java使用imageio、awt生成图片验证码
1.生成验证码工具类 public class CheckCodeTool { private Integer width = 80; private Integer height = 38; pub ...
- Java开发高性能网站需要关注的事
转自:http://www.javabloger.com/java-development-concern-those-things/ 近期各家IT媒体举办的业内技术大会让很多网站都在披露自己的技术内 ...