Question

In a MVC3 app, I need a global filter to get a value and before any controller is executed, however how can I get later that value from a Controller?

public class MyGlobalFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        int i = 10;
    }
}
Was it helpful?

Solution 2

I found that it's possible to set a new ViewBag value by the action controller context as the following code

filterContext.Controller.ViewBag.MyNewValue = 1

OTHER TIPS

You can use the HttpContext.Items

public class MyGlobalFilter : ActionFilterAttribute
{
    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        HttpContext.Current.Items["I"] = 10;
    }
}
public class MyController : Controller
{
    private int _i;
    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        _i = (int)HttpContext.Items["I"];
    }
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top