I have this filter:
public class ViewBagFilter : IActionFilter
{
private readonly ApplicationDbContext db;
public ViewBagFilter(ApplicationDbContext _dbContext)
{
db = _dbContext;
}
public void OnActionExecuting(ActionExecutingContext context)
{
var controller = context.Controller as Controller;
var dataFromDb = {db-query};
controller.ViewBag.Example = dataFromDb;
}
public void OnActionExecuted(ActionExecutedContext context)
{
// do something after the action executes
}
}
... that I am including in Startup.cs
like this:
services.AddMvc(options =>
{
options.Filters.Add(typeof(ViewBagFilter));
});
But when I navigate to any of the Identity razor pages, ViewBag.Example
does not exist.
How can I make it available to razor pages as well?
This doesn't work:
services.AddRazorPages(options =>
{
options.Filters.Add(typeof(ViewBagFilter));
});
Filters in Razor Pages are different to MVC. You will need to create a Razor Pages version: https://www.learnrazorpages.com/razor-pages/filters
Note that the dynamic ViewBag
type is not available in the Razor Pages PageModel
type.