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
    }

    }

  • 相关阅读:
    小三学算术
    The Balance
    扫描线
    2019牛客暑期多校训练营(第九场)
    后缀数组
    Manacher
    局部变量和全局变量的区别
    2386:Lake Counting-poj
    简单背包问题(0032)-swust oj
    编程中易犯错误集锦-持续更新。。。。。
  • 原文地址:https://www.cnblogs.com/yufan27209/p/6811407.html
Copyright © 2011-2022 走看看