简体   繁体   English

如何从OnActionExecuting操作过滤器返回值

[英]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.

相关问题 如何防止从Controller继承的类的OnActionExecuting中执行动作 - How to prevent action execution in OnActionExecuting of class that is inhereted from the Controller Web Api - 如何直接从OnActionExecuting过滤器停止Web管道 - Web Api - how to stop the web pipeline directly from an OnActionExecuting Filter 在进行Web API的OnActionExecuting调用期间-如何从路由字符串映射到控制器/操作类以读取操作属性 - During OnActionExecuting for Web API call - How to map to controller/action class from route string to read action attributes 如何从ASP.NET Web API OnActionExecuting Filter方法中提取表单数据 - How can I extract form data from within the ASP.NET Web API OnActionExecuting Filter method 我如何返回Json结果+ Web API +验证模型+ ActionFilters + OnActionExecuting方法 - How can i return Json Result + Web api + validate model + actionfilters +OnActionExecuting method 如何从WebApi动作返回html页面? - How to return html page from WebApi action? 如何在Web API的动作过滤器中获取控制器值 - How to get controller value in Action filter in Web API 将ActionFilter.OnActionExecuting()中的对象传递给ApiController - Pass an object from ActionFilter.OnActionExecuting() to an ApiController 即使OnActionExecuting返回了响应,也会触发OnActionExecuted - Fire OnActionExecuted even if response is returned from OnActionExecuting 我如何验证OnActionExecuting方法 - How can i validate OnActionExecuting methode
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM