MVC中的Action过滤器
Action过滤器可以用在调用动作方法之前或之后,执行一些特殊的逻辑,比如用登录验证:
Action过滤器实现IActionFilter接口,该接口有两个方法:
public interface IActionFilter
{
void OnActionExecuted(ActionExecutedContext filterContext);
void OnActionExecuting(ActionExecutingContext filterContext);
}
我们先创建一个BaseController,在这个基类中重写OnActionExecuting方法,用于让其他的控制器继承此基类。如:
using System;
using System.Collections.Generic;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Script.Serialization;
using XFK.PICCInsureTravel.Common;
using XinfuMall.XinfuWeb.PublicClass; namespace XFK.PICCInsureTravel.Controllers
{
public abstract class BaseController : Controller
{
public JavaScriptSerializer js = new JavaScriptSerializer();
public LogHelper log = null;
public UserSession user { get; set; }
public EnterpriseAccountSession enterprise { get; set; }
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
base.OnActionExecuting(filterContext);
LogHelper.WriteLog("进入BaseController");
TempData["xinfuUrl"] = System.Configuration.ConfigurationManager.AppSettings["xinfuUrl"];
if (XFK.Infrastructure.Util.SessionManager.Read(UserUtil.user_sessionStr) != null)
{
user = XFK.Infrastructure.Util.SessionManager.Read(UserUtil.user_sessionStr) as UserSession;
object enterpriseId = XFK.Infrastructure.Util.SessionManager.Read(EnterpriseUtil.enterprise_sessionId);
enterprise = new EnterpriseAccountSession();
enterprise.Id = Convert.ToInt32(enterpriseId);
ViewBag.UserName = user.AccountName;
ViewBag.Enterpriseid = Convert.ToInt32(enterpriseId);
var limitEnterprise = String.IsNullOrEmpty(ConfigurationManager.AppSettings["limitEnterprise"]) ? "" : ConfigurationManager.AppSettings["limitEnterprise"];
if (limitEnterprise.Split(',').Contains(enterprise.Id.ToString()))
{
System.Web.HttpContext.Current.Response.Write("对不起,所属的企业没有订购此产品的权限!");
}
}
else
{
filterContext.Result = new RedirectResult(ConfigurationManager.AppSettings["xinfuUrl"] + "mall/Login?originUrl=" + HttpUtility.UrlEncode(Request.Url.ToString()));
LogHelper.WriteLog("当前访问的路径是:" + ConfigurationManager.AppSettings["xinfuUrl"] + "mall/Login?originUrl=" + HttpUtility.UrlEncode(Request.Url.ToString()));
}
}
}
}
注意,当验证不通过时,我们给filterContext.Result赋值,用于跳转页面。这个filterContext.Result属性是一个ActionResult类型的子集---RedirectResult:

当执行到filterContext.Result时,我们给它赋值以后,便会执行跳转。
这个BaseController建好以后,下面的工作就是在其他控制器中使用了:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc; namespace XFK.PICCInsureTravel.Controllers
{
public class HomeController : BaseController
{
public ActionResult Index()
{
return View();
}
}
}
参考:http://skybirdzw.blog.163.com/blog/static/72570626201781465310313/
MVC中的Action过滤器的更多相关文章
- MVC中使用Action全局过滤器出现:网页无法正常运作 将您重定向的次数过多。解决办法
前言当我们访问某个网站的时候需要检测用户是否已经登录(通过Session是否为null),我们知道在WebForm中可以定义一个BasePage类让他继承System.Web.UI.Page,重写它的 ...
- MVC中的错误过滤器无法拦截URL路径错误的解决办法
“/”应用程序中的服务器错误. 无法找到资源. 说明: HTTP 404.您正在查找的资源(或者它的一个依赖项)可能已被移除,或其名称已更改,或暂时不可用.请检查以下 URL 并确保其拼写正确. 请求 ...
- mvc中的action验证登录(ActionFilterAttribute)
方法一 : 1.创建一个全局action过滤器 (在appstart 的filterconfig中注册 filters.Add(new LoginAttribute());) 2.不需要登 ...
- ASP.NET MVC中在Action获取提交的表单数据方法总结 (4种方法,转载备忘)
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- ASP.NET MVC中在Action获取提交的表单数据方法
有Index视图如下: 视图代码如下: <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Mas ...
- MVC中@Html.Action的用法
MVC项目中如果有公共部分的代码就可以单独拿出来作为控件来用(比如头部和底部代码).跟ASP.NET中的ASCX实现的效果一样,但MVC比它方便的多. 想要实现该效果,需要知道@Html.Action ...
- MVC中@Html.Action的用法(类似自定义控件)
MVC项目中如果有公共部分的代码就可以单独拿出来作为控件来用(比如头部和底部代码).跟ASP.NET中的ASCX实现的效果一样,但MVC比它方便的多. 一.@Html.Action的用法 @Html. ...
- .NET MVC中登陆授权过滤器的使用
1.写个类LoginAuthorityAttribute,继承自AuthorizeAttribute using System; using System.Collections.Generic; u ...
- mvc中在Action里调用另一个Action
今天做东西时发现一个新东西.即在一个Action调用另一Action.前提是同一个控制器.(没在一个控制里的没试过) 调用方法: public ActionResult Test1(){ //to ...
随机推荐
- caps lock 映射成 esc,右Ctrl映射右移
xmodmap -e 'clear Lock' -e 'keycode 0x42 = Escape' xmodmap -e 'clear Lock' -e 'keycode 105= Right'
- SpringCloud(一)之微服务核心组件Eureka(注册中心)的介绍和使用
一 Eureka服务治理体系1.1 服务治理服务治理是微服务架构中最为核心和基础的模块,它主要用来实现各个微服务实例的自动化注册和发现. Spring Cloud Eureka是Spring Clou ...
- <JavaScript>“浏览器模式”和“文档模式”之间的区别
只有IE浏览器中才会有“浏览器模式”和“文档模式”,兼容性视图涉及两个重要的功能便是“浏览器模式[browser mode]”和“文档模式[document mode]”,在IE8/IE9中按F12键 ...
- redis 的使用 及 配置文件解读
redis-server命令 redis-server /usr/local/redis/conf/redis.conf #加配置文件绝对路径启动redis服务 redis-server /usr/l ...
- LC 820. Short Encoding of Words
Given a list of words, we may encode it by writing a reference string S and a list of indexes A. For ...
- LC 756. Pyramid Transition Matrix
We are stacking blocks to form a pyramid. Each block has a color which is a one letter string, like ...
- layui-简单的登录注册界面【转载】
register.html 源代码: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 ...
- SPARQL查询语句整理
本文大多内容来自Joshua Taylor的回答 https://stackoverflow.com/users/1281433/joshua-taylor 查询子类或等价关系 https://sta ...
- Windows 10 删除资源管理器中7个文件夹
Windows 10 安装完成之后 ,在资源管理器中会存在 7 个文件夹,他们分别是:图片.视频.下载.音乐.桌面.文档.3D对象. 我们可以通过修改注册表的方式,隐藏这7个文件夹.相关注册表内容如下 ...
- 3分钟Markdown快速入门与使用
Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. 注意:图片为效果图 1 标题 #开头代表标题,几个#号代表几级,最高支持六级标题 ...