Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

aspnet vNext ActionFilter and TempData

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.

like image 302
vfabre Avatar asked May 25 '15 06:05

vfabre


People also ask

What is difference between ViewBag ViewData and TempData?

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.

What is TempData used for?

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.

What is Actionfilter?

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.

Can we use TempData in Web API?

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.


1 Answers

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(...)
    
like image 194
Kiran Avatar answered Sep 25 '22 23:09

Kiran