Web APIでのActionFilterとExceptionFilter
10719 ワード
本文は重点会標紅である.
バックグラウンド要件は,actionに入る前にsign検証を行い,どうせエラーが発生した場合にlogを記録し,エラーのjson形式情報を返す.
ApiController eg.
バックグラウンド要件は,actionに入る前にsign検証を行い,どうせエラーが発生した場合にlogを記録し,エラーのjson形式情報を返す.
#region ActionFilter add by caoheyang 20150319
/// <summary>
/// sign add by caoheyang 20150318
/// </summary>
public class SignOpenApiAttribute : System.Web.Http.Filters.ActionFilterAttribute
{
/// <summary>
/// OnActionExecuting sign add by caoheyang 20150318
/// </summary>
/// <param name="actionContext"></param>
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
lock (actionContext)
{
dynamic paramodel = actionContext.ActionArguments["paramodel"]; //
if (actionContext.ModelState.Count > 0 || paramodel == null) // ,
actionContext.Response = actionContext.ActionDescriptor.ResultConverter.Convert
(actionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.ParaError));
IGroupProvider groupProvider = new GroupProvider();
GroupApiConfigModel groupCofigInfo = groupProvider.GetGroupApiConfigByAppKey(paramodel.app_key, paramodel.v).Data;
if (groupCofigInfo != null && groupCofigInfo.IsValid == 1)// , appkey
{
string signStr = groupCofigInfo.AppSecret + "app_key=" + paramodel.app_key + "timestamp" + paramodel.timestamp + "v=" + paramodel.v + groupCofigInfo.AppSecret;
string sign = MD5.Encrypt(signStr);
paramodel.group = ParseHelper.ToInt(groupCofigInfo.GroupId, 0);
actionContext.ActionArguments["paramodel"] = paramodel; ;
if (sign != paramodel.sign) //sign ,
actionContext.Response = actionContext.ActionDescriptor.ResultConverter.Convert
(actionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.SignError)); // apicontroller fillter action 。
}
else
actionContext.Response = actionContext.ActionDescriptor.ResultConverter.Convert
(actionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.SignError)); //sign ,
}
}
}
#endregion
#region ExceptionFilter add by caoheyang 20150319
/// <summary>
/// add by caoheyang 20150319 , log /// </summary>
public class OpenApiHandleErrorAttribute : ExceptionFilterAttribute
{
/// <summary>
/// add by caoheyang 20150205 /// </summary>
/// <param name="filterContext"> ControllerContext</param>
public override void OnException(HttpActionExecutedContext filterContext)
{
LogHelper.LogWriterFromFilter(filterContext.Exception);
}
}
/// <summary> action , ,
/// action , , add by caoheyang 20150319
/// </summary>
public class OpenApiActionErrorAttribute : ExceptionFilterAttribute
{
/// <summary>
/// add by caoheyang 20150205
/// </summary>
/// <param name="filterContext"> ControllerContext</param>
public override void OnException(HttpActionExecutedContext filterContext)
{
filterContext.Response = filterContext.ActionContext.ActionDescriptor.ResultConverter.
Convert(filterContext.ActionContext.ControllerContext, ResultModel<object>.Conclude(OrderApiStatusType.SystemError));
// apicontroller fillter action 。
} } #endregion
ApiController eg.
// POSR: Order GetStatus paramodel paramodel
/// <summary>
/// add by caoheyang 20150316
/// </summary>
/// <returns></returns>
[HttpPost]
[SignOpenApi] //sign ,sign add by caoheyang 201503167
[OpenApiActionError] // add by caoheyang ,
public ResultModel<object> GetStatus(ParaModel<GetStatusPM_OpenApi> paramodel)
{
}