asp.net mvc,基于aop实现的接口访问统计、接口缓存等

其实asp.net 上aop现有的框架应该蛮多的,比如静态注入式的PostSharp(新版本好像已经商业化了,旧版本又不支持.net4.0+),或者通过反射的(性能会降低)。

本文则是通过mvc其中一种方法拦截器ActionFilter(参考网上已经有很多类似例子)。

首先新建一个日志控制类,命名为ApiLogAttribute,继承于ActionFilterAttribute

/// <summary>
    /// 记录访问日志以及站点安全检查
    /// </summary>
    public class ApiLogAttribute : ActionFilterAttribute
    {
        #region 属性

         private string logType;
         public ApiLogAttribute(string type)
        {
            logType = type;
        }

        #endregion

        public override void OnActionExecuting(HttpActionContext actionContext)
        {
            if (string.IsNullOrEmpty(logType))
                return;
            AccessSummaryHelper.AddOneVisit(logType);//自己实现接口访问量统计的代码

           
            //这里可以约定 增加一些接口校验,避免无效的请求。return;
                
            
            //校验不通过的,则直接返回,不会进入方法体内
            actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, new ApiResult{ code=1,Msg="无效的请求。"});
 } 
}

 

接着,再建一个接口方法缓存类,命名为ApiCacheAttribute,继承自ActionFilterAttribute

public class ApiCacheAttribute : ActionFilterAttribute//,System.Web.Mvc.ActionFilterAttribute
    {
    //设置缓存的时间,默认为1分钟
private CacheTimeOption cacheTime ; public ApiCacheAttribute(CacheTimeOption cachetime = CacheTimeOption.OneMinute) { cacheTime = cachetime; } public override void OnActionExecuting(HttpActionContext actionContext) { base.OnActionExecuting(actionContext); string cacheKey = HttpContext.Current.Request.RawUrl; ApiResult ret = CacheHelper.Get<ApiResult>(cacheKey); //在action执行前终止请求时,应该使用填充方法Response,将不返回action方法体。 if (ret != null) { actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.OK, ret); } } public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext) { string cacheKey = HttpContext.Current.Request.RawUrl; var obj = ((ObjectContent)(actionExecutedContext.Response.Content)).Value; if (obj != null) { ApiResult ret = obj as ApiResult; if (ret != null) { CacheHelper.Set(cacheKey, ret, cacheTime); } } } }

注意上面 ApiResult为自定义的接口返回类。

public class ApiResult
{
        public int Code{ get; set; }

        public string Msg { get; set; } 

public object Data { get; set; }
}

最后在具体的类接口上调用如下:

 public class XXController : ApiController
    {
        
        [HttpGet]
        [ApiCache]
        [ApiLog("1.1")]
        public ApiResult GetXX()
        {
                //业务代码略
        }
}

 

 

转载于:https://www.cnblogs.com/contraII/p/3822467.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值