Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to access Request.Properties in ActionExecutingContext

How to access Request.Properties in ActionExecutingContext?

public class UserFilter : ActionFilterAttribute
{
    public void OnActionExecuting(ActionExecutingContext actionContext)
    {
        // Properties is not part of the Request here, so I can't access it
        // Here Request is of type System.Web.HttpRequestBase
        actionContext.HttpContext.Request.Properties.Add("UserData", new UserData());
    }
}

I can do it in ApiController:

public class HomeController : ApiController
{
    public HomeController()
    {
        // Here I can do it (here Request is of type
        // System.Net.Http.HttpRequestMessage
        this.Request.Properties.Add("UserData", new UserData());
    }
}
like image 455
one_mile_run Avatar asked Nov 09 '22 22:11

one_mile_run


1 Answers

For ApiController you need use another ActionFilterAttribute (located in System.Web.Http.Controllers namespace):

    using System.Web.Http.Controllers;
    using System.Web.Http.Filters;

    public class UserFilter : ActionFilterAttribute
    {
        public override void OnActionExecuting(HttpActionContext actionContext)
        {
                    // Properties is part of the Request here, you can access it
                    // actionContext.Request.Properties
        }
    }
like image 148
Evgeniy Mironov Avatar answered Nov 14 '22 23:11

Evgeniy Mironov