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
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;
}
}
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"];
}
}