I'm trying to create an ActionFilter in aspnet vNext. Within this filter I want to access to the TempData and the ViewData (both available in Controller in previous versions).I override the method
public override void OnActionExecuting(ActionExecutingContext filterContext)
Into the filterContext
I have the controller but is an object
instead of ControllerBase
. I was expected a ControllerBase
because in previous versions of MVC the ControllerContext
(the base class of ActionExecutingContext
) was a ControllerBase
, here is the source code in codeplex. I understand that this could be because the POCO controllers.
So, the question is, how can access to the TempData and the ViewData if the controller is an object. Simply doing a downcasting (something like this (Controller)filterContext.Controller
) or there's a best way to do it.
Update
That I want to achieve if explain it in this blog post but with aspnet 5.
To summarize, ViewBag and ViewData are used to pass the data from Controller action to View and TempData is used to pass the data from action to another action or one Controller to another Controller.
TempData is used to transfer data from the view to the controller, the controller to the view, or from an action method to another action method of the same or a different controller. TempData temporarily saves data and deletes it automatically after a value is recovered.
Action filters contain logic that is executed before and after a controller action executes. You can use an action filter, for instance, to modify the view data that a controller action returns. Result filters contain logic that is executed before and after a view result is executed.
It uses the keyvalue for passing the data and it has a need for typecasting. TempData: TempData is a dictionary that is derived from the TempDataDictionary class. The tempData Dictionary object persists only from one request to the next. You can mark one or more keys for retention using the keep method.
To access TempData from within an action filter, you can get the service called ITempDataDictionary
from DI.
To get this service from DI, you could either do something like actionContext.HttpContext.RequestServices.GetRequiredService<ITempDataDictionary>()
from within your OnActionExecuting
method. You could also use construction inject if you like by using ServiceFilterAttribute.
NOTE:
TempData by default depends on Session
feature(i.e TempData's data is stored in Session) and so you need to few things to get it working.
Reference Microsoft.AspNet.Session
and Microsoft.Framework.Caching.Memory
packages.
In your ConfigureServices
method, do the following:
services.AddCaching();
services.AddSession();
In your Configure
method, register the Session
middleware (this is the one which creates/attaches a session to the incoming requests) and do it before registering MVC.
app.UseSession();
app.UseMvc(...)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With