62,041
社区成员
发帖
与我相关
我的任务
分享
[Route("User/Add")]
[HttpPost]
public dynamic AddUser([FromBody] Product p)
{
if (p == null)
{
throw new Exception("Product为空");
}
list.Add(p);
return Respose.OK(p);
}
public override void OnActionExecuted(HttpActionExecutedContext actionExecutedContext)
{
var MonLog = actionExecutedContext.Request.Properties[Key] as WebApiMonitorLog;
MonLog.ExecuteEndTime = DateTime.Now;
if (actionExecutedContext.Exception != null)
{
MonLog.Exception = actionExecutedContext.Exception.Message;
var base_resp = new { base_resp = new { errcode = -1, errmsg = MonLog.Exception } };
MonLog.Respose = Newtonsoft.Json.JsonConvert.SerializeObject(base_resp);
actionExecutedContext.Response = actionExecutedContext.Request.CreateResponse(HttpStatusCode.OK, base_resp);
}
else
{
var c = actionExecutedContext.Response;
if (c != null)
{
var oc = c.Content as ObjectContent;
if (oc != null)
{
MonLog.Respose = oc.Value;
}
}
base.OnActionExecuted(actionExecutedContext);
}
}
其中 MonLog.Respose就是action的返回值..你可以在这里做处理替换..
public override void OnActionExecuting(System.Web.Http.Controllers.HttpActionContext actionContext)
{
base.OnActionExecuting(actionContext);
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
//想在这个地方吧 actionContext返回给客户端的数据拿出来
//再替换掉Null
// 重新打包回传的讯息
}
public class ApiErrorHandleAttribute : ExceptionFilterAttribute
{
public override void OnException(HttpActionExecutedContext actionExecutedContext)
{
base.OnException(actionExecutedContext);
var result = new apiResult<object>()
{
code = HttpStatusCode.BadRequest, //状态码
msg = actionExecutedContext.Exception.Message //信息
..................自己封装这个apiResult
};
// 重新打包回传的讯息
actionExecutedContext.Response = actionExecutedContext.Request
.CreateResponse(result.code, result);
}
}