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
    }

    }

  • 相关阅读:
    i春秋 可恶的黑客
    bugku 变量1
    开源Odoo ERP 13.2版本发行说明(译文+原文)
    Java数学运算
    SET DYNAMICS 365 COLORS AND LOGO USING THEMES
    Use SQL to Query Data from CDS and Dynamics 365 CE
    SAP四代增强实现:VA01销售订单复制项目文本时不需要显示文本框和回车
    ABAP 动态备份自建表数据到新表(自建表有数据的情况下要改字段长度或者其他)
    NTFS ADS(备用数据流)
    Windows RestartManeger重启管理器
  • 原文地址:https://www.cnblogs.com/yufan27209/p/6811407.html
Copyright © 2011-2022 走看看