Search code examples
c#.net-coreviewbagaction-filtertempdata

Pass TempData to ActionFilter RedirectToAction


I want to set a ViewBag for second an action from the first action by using ActionFilter.

In the first Action i do the following :

TempData["Test"] = "Test"; 
return RedirectToAction("Action2", new { values = values });

Then in IActionFilter :

public class HelpertestActionFilter : IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext context)
    {
    }

    public void OnActionExecuting(ActionExecutingContext context)
    {
        var controller = context.Controller as Controller;
        if (controller != null)
        {
            if (controller.TempData["Test"] != null)
            {
                controller.ViewBag.Notification = controller.TempData["Test"];
            }
        }
    }
}

But in ActionFilter OnActionExecuting, TempData["Test"] is always null.

I have followed this article


Solution

  • After some try, there is no errors in my code, except in the startup configuration.

    In Startup.Configure() the app.UseCookiePolicy() has to be after app.UseMVC() to work as expected.