.NET ActionFilterAttribute等

摘要:
publicoverridevoidOnException(HttpActionExecutedContextactionExecutedContext){//加LOGactionExecutedContext.Exception//2.返回调用方具体的异常信息if(actionExecutedContext.ExceptionisNotImplementedException){actionEx

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
}

}

免责声明:文章转载自《.NET ActionFilterAttribute等》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇JUC--volatiley&CAS号码字符串与BCD编码互转 c#下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

Eclipse paho学习笔记

Eclipse Paho java 支持java和android 两个客户端异步和同步, MqttAsyncClient,MqttClient 其中 后者是前者的包装同步器,特效除了5.0都支持 有两个版本可以相互替换 <dependency> <groupId>org.eclipse.paho</...

ASP.Net Core 中使用Zookeeper搭建分布式环境中的配置中心系列一:使用Zookeeper.Net组件演示基本的操作

前言:马上要过年了,祝大家新年快乐!在过年回家前分享一篇关于Zookeeper的文章,我们都知道现在微服务盛行,大数据、分布式系统中经常会使用到Zookeeper,它是微服务、分布式系统中必不可少的分布式协调框架。它的作用体现在分布式系统中解决了配置中心的问题,以及解决了在分布式环境中不同进程之间争夺资源的问题,也就是分布式锁的功能以及分布式消息队列功能等...

Java Properties 类读取配置文件信息

在我们平时写程序的时候,有些参数是经常改变的,而这种改变不是我们预知的。比如说我们开发了一个操作数据库的模块,在开发的时候我们连接本地的数据库那么IP,数据库名称,表名称,数据库主机等信息是我们本地的,要使得这个操作数据的模块具有通用性,那么以上信息就不能写死在程序里。通常我们的做法是用配置文件来解决。 各种语言都有自己所支持的配置文件类型。比如Pytho...

阿里云OSS存储

1.accessKeyId与accessKeySecret是由系统分配给用户的,称为ID对,用于标识用户,为访问OSS做签名验证。 2.Bucket是OSS上的命名空间,相当于数据的容器,可以存储若干数据实体(Object) 你可以按照下面的代码新建一个Bucket: /// <summary> /// 新建...

springboot多环境下的自定义配置文件,并读取到常量

import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.context.annotation.PropertySource; import org.springframework.stereotype.Com...

CString转string

如题,找了半天。。。 1 //CString转string 2 3 USES_CONVERSION; 4 CString temp; 5 temp = _T("kjdsaflkjdlfkj"); 6 char* s_char = W2A(temp); 7 string ss = s_char;...