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;
    }
}
有帮助吗?

解决方案 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

其他提示

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"];
    }
}
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top