[英]how to return value from OnActionExecuting action filter
this is my API action filter,i want to be able to return the ccaccount obj 这是我的API操作过滤器,我希望能够返回ccaccount obj
on good login result to the calling method 登录方法调用效果良好
any idea how ? 任何想法如何?
thanks 谢谢
public class CAPILoginFilter : ActionFilterAttribute
{
public override void OnActionExecuting(HttpActionContext i_actionContext)
{
CRequestBase request = (CRequestBase)i_actionContext.ActionArguments["i_request"];
CCAccount account = CAPILogin.AccountIDGetOBJ(request.UserName, request.Password);
if(account == null)
{
throw new HttpResponseException(new HttpResponseMessage(HttpStatusCode.Unauthorized));
}
else
{
//return account object to calling method here
}
}
}
It is not possible to pass a value back from OnActionExecuting but you can set properties in the OnActionExecuting event that can be read by the method that caused it to fire. 无法将值从OnActionExecuting传递回去,但是可以在OnActionExecuting事件中设置属性,该属性可以由引发它的方法读取。
In your OnActionExecuting event create a new property on request object. 在您的OnActionExecuting事件中,在请求对象上创建一个新属性。
context.Request.Properties["X"] = variableX;
This will be readable by the method that caused the event to fire like this 这样导致事件触发的方法将可以读取此信息
if (this.Request.Properties.ContainsKey("X"))
{
string x = this.Request.Properties["X"].ToString();
// Use x here
}
Dave 戴夫
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.