惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

大猫的无限游戏
大猫的无限游戏
aimingoo的专栏
aimingoo的专栏
I
InfoQ
B
Blog RSS Feed
D
DataBreaches.Net
S
SegmentFault 最新的问题
P
Proofpoint News Feed
A
About on SuperTechFans
WordPress大学
WordPress大学
Hugging Face - Blog
Hugging Face - Blog
博客园 - 司徒正美
小众软件
小众软件
博客园 - Franky
有赞技术团队
有赞技术团队
D
Docker
T
Tailwind CSS Blog
雷峰网
雷峰网
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Blog — PlanetScale
Blog — PlanetScale
酷 壳 – CoolShell
酷 壳 – CoolShell
B
Blog
V
Visual Studio Blog
宝玉的分享
宝玉的分享
爱范儿
爱范儿

博客园 - yufan27209

博文阅读密码验证 - 博客园 JVM调优 【转】中文分词之HMM模型详解 xwiki enterprise 8.4.5使用https步骤 博文阅读密码验证 - 博客园 dubbo和shiro的整合,在服务端做权限验证 电商课题:分布式锁 Export large data from Gridview and Datareader to an Excel file using C# 博文阅读密码验证 - 博客园 sap学习笔记 设计模式读书笔记-----访问者模式DEMO修改 用户中心 - 博客园 MYSQL实现上一条下一条功能 SVN基本配置文件 单表60亿记录等大数据场景的MySQL优化和运维之道 MSSQL镜像—无见证服务器 风险控制-防刷 开发中两款工具 Remote Desktop Organizer 和zabbix EasyUI datagrid 编辑框焦点
.NET ActionFilterAttribute等
yufan27209 · 2017-05-05 · via 博客园 - yufan27209

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
}

}