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
    }

    }

  • 相关阅读:
    VPC/VM/VBOX安装GHOST版的无法启动系统
    在虚拟机中使用Ghost系统盘安装
    用vmware安装gho文件心得
    VC中Error spawning cl.exe错误的解决方法.
    C语言屏幕打印,再删除打印的内容
    bat根据星期启动程序
    ARP命令详解
    bat生成vbs通过注册表禁用或启用USB端口
    OracleDesigner学习笔记1――安装篇
    MarkMonitor 目前最安全的域名注册商,因此,世界500强网站中的22%域名托管于markmonitor公司
  • 原文地址:https://www.cnblogs.com/yufan27209/p/6811407.html
Copyright © 2011-2022 走看看