如何在 IActionFilter MVC6 中设置 ViewBag?

How to set ViewBag in IActionFilter MVC6?

public class MAttribute : IActionFilter
{

public void OnActionExecuted(ActionExecutedContext actionContext)
    {
        string searchData = "";
        actionContext.Controller.ViewBag.SearchItem = searchData;
        //object not included in the definition
    }

}

当我重写 IActionFilter 时,它提示 'object not included in the definition',那么如何在 IActionFilter MVC6 中设置 ViewBag?

尝试从 ActionFilterAttribute 继承 我认为你想要的方法签名是 OnActionExecuting(ActionExecutingContext)

有关详细信息,请参阅:http://www.strathweb.com/2015/06/action-filters-service-filters-type-filters-asp-net-5-mvc-6/

编辑:如果您还没有在顶部添加 using Microsoft.AspNetCore.Mvc.Filters;,您可能还想添加。