MVC过滤器之 OnActionExcuted

时间:2021-03-25 15:34:54

Controller里

[SendMessage]

public Action SendSmsMessage()

{

var resultExtendInfo=new ResultExtendInfo(){Phone="13434343434"};

if(true)

return new G2JsonResult(true,"",resultExtendInfo);

else

return new G2JsonResult(false,"",null);

}

public class ResultExtendInfo

{

public string Phone{get;set;}

}

public class G2JsonResult : ActionResult
{

public bool Success { get; set; }

public object Data { get; set; }

public string Message { get; set; }

public G2JsonResult(bool isSucc, string message = "",object data = null)
{
this.Success = (isSucc);
this.Message =message;

this.Data = data;
}

}

在SendMessageAttribute.cs里

public class SendMessageAttribute : ActionFilterAttribute
{

/// <summary>
/// actin执行后
/// </summary>
/// <param name="filterContext"></param>
public override void OnActionExecuted(ActionExecutedContext filterContext)
{
base.OnActionExecuted(filterContext);

var g2JsonResult=filterContext.Result as G2JsonResult;

if(g2JsonResult.Success)

{

var extendInfo=geJsonResult.Data as ResultExtendInfo;

//这时就取到相要的数据了

}

}

}