使用无注解属性的过滤

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了使用无注解属性的过滤相关的知识,希望对你有一定的参考价值。

mvc中使用过滤器常规的办法是使用注解属性。然而还有另外一种办法,Controller类也实现了IAuthorizationFilter、IActionFilter、IResultFilter和IExceptionFilter接口。所以可以在

Controller类中直接重写相应的方法就可以使该Controller下的所有Action就被应用于所重写的过滤。

public class HomeController : Controller
{
private Stopwatch timer;
[Authorize(Users = "admin")]
public string Index()
{
return "This is the Index action on the home controller";
}
[GoogleAuth]
[Authorize(Users ="[email protected]")]
public string List() {
return "this is the List action on the Home controller";
}
//[RangeException]
[HandleError(ExceptionType =typeof(ArgumentOutOfRangeException),View ="RangeError")]
public string RangeTest(int id) {
if (id > 100)
{
return String.Format("The id value is:{0}", id);
}
else {
throw new ArgumentOutOfRangeException("id",id,"");
}
}

public string FilterTest() {
return "this is the filterTest action";
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
timer = Stopwatch.StartNew();
}

protected override void OnResultExecuted(ResultExecutedContext filterContext)
{
timer.Stop();
filterContext.HttpContext.Response.Write(string.Format("<div> Total elapsed time:{0}</div>",timer.Elapsed.TotalSeconds));
}
}

 

以上是关于使用无注解属性的过滤的主要内容,如果未能解决你的问题,请参考以下文章

Jackson全面解析--注解全讲解九(过滤无用信息@JsonInclude)

FastJson序列化时过滤字段(属性)的方法

@WebFilter注解

springMvc将对象json返回时自动忽略掉对象中的特定属性的注解方式

@WebFilter注解

json常用的注解