zoukankan      html  css  js  c++  java
  • .NET ActionFilterAttribute等

    public override void OnException(HttpActionExecutedContext actionExecutedContext)
    {
    //加LOG actionExecutedContext.Exception

    //2.返回调用方具体的异常信息
    if (actionExecutedContext.Exception is NotImplementedException)
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
    }
    else if (actionExecutedContext.Exception is TimeoutException)
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.RequestTimeout);
    }
    //.....这里可以根据项目需要返回到客户端特定的状态码。如果找不到相应的异常,统一返回服务端错误500
    else
    {
    actionExecutedContext.Response = new HttpResponseMessage(HttpStatusCode.InternalServerError);
    }

    base.OnException(actionExecutedContext);
    }

    public class TokenAuthAttribute : ActionFilterAttribute
    {
    public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
    {
    base.OnActionExecuting(actionContext);

    //POST的数据
    using (Stream stream = actionContext.Request.Content.ReadAsStreamAsync().Result)
    {
    if (stream.Length > 0)
    {
    stream.Position = 0;
    string data = string.Empty;
    using (StreamReader reader = new StreamReader(stream, Encoding.UTF8))
    {
    data = reader.ReadToEnd();
    }
    //加LOG
    }
    }

    object token = null;
    actionContext.ActionArguments.TryGetValue("token", out token);
    //string token = string.Concat(HttpContext.Current.Request.Form["token"]);
    ServiceK3Result result = new ServiceK3Result();
    if (string.Concat(token).Equals("a"))
    {
    result.code = "401";
    result.message = "IP受限制,请联系接口管理员!";
    result.redirect = "";
    actionContext.Response = actionContext.Request.CreateResponse(HttpStatusCode.Unauthorized, JsonHelper.ObjectToJsonString(result));
    }
    }

    public async override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
    {
    base.OnActionExecuted(actionExecutedContext);
    //加结束LOG
    }

    }

  • 相关阅读:
    Android中Handler原理
    微软柯塔娜(Cortana)的一句名言
    C# 单例模式的五种写法
    HTTP Status 404
    PLSQL中显示Cursor、隐示Cursor、动态Ref Cursor差别
    Android 开发之集成百度地图的定位与地图展示
    iOS知识点汇总
    优化报表系统结构之报表server计算
    淘宝中间的一像素线(手机端)
    解决yum升级的问题“There was a problem importing one of the Python modules”
  • 原文地址:https://www.cnblogs.com/yufan27209/p/6811407.html
Copyright © 2011-2022 走看看