您好,我想为我的应用程序中的每个控制器创建自定义ActionFilterattribute,此属性应该设置一些ViewBag值. ActionFilterattribute是否适合它,以及如何在ActionFilterattribute中访问viewbag?
解决方法
你可以这样做
public class SomeMsgAttribute : Filterattribute,IResultFilter { public void OnResultExecuted(ResultExecutedContext filterContext) { } public void OnResultExecuting(ResultExecutingContext filterContext) { filterContext.Controller.ViewBag.Msg= "Hello"; } }
使用:
[SomeMsg] public ActionResult Index() { return View(); }