How to pass a variable from ActionFilter to Action Controller in C # MVC?

By accepting a simple action filter that checks if a user is registered and if his user ID is being received.

public class LoginFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // Authenticate (somehow) and retrieve the ID int id = Authentication.SomeMethod(); // Pass the ID through to the controller? ..... } } 

How could I pass this identifier before my action with the controller?

 [LoginFilter] public class Dashboard : Controller { public ActionResult Index() { // I'd like to be able to use the ID from the LoginFilter here int id = .... } } 

Is there an equivalent ViewBag that would allow me to do this? Or some other method that allows me to pass variables and objects between a filter and a controller action?

+5
source share
3 answers

You can use ViewData/ViewBag as follows:

1.) Using ViewData

NOTE: In the case of ViewData, you need to take one step that you must give to this type

 public class LoginFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // Authenticate (somehow) and retrieve the ID int idValue = Authentication.SomeMethod(); // Pass the ID through to the controller? filterContext.Controller.ViewData.Add("Id", idValue); } } 

And then in the controller function

 [LoginFilter] public class Dashboard : Controller { public ActionResult Index() { // I'd like to be able to use the ID from the LoginFilter here int id = (int)ViewData["Id"]; } } 

2.) Using ViewBag

 public class LoginFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // Authenticate (somehow) and retrieve the ID int idValue = Authentication.SomeMethod(); // Pass the ID through to the controller? filterContext.Controller.ViewBag.Id = idValue; } } 

And then in the controller

 [LoginFilter] public class Dashboard : Controller { public ActionResult Index() { // I'd like to be able to use the ID from the LoginFilter here int id = ViewBag.Id; } } 
+6
source

You can use ViewBag by doing:

 filterContext.Controller.ViewBag.Id = id; 

which should do this as soon as you do filterContext.Controller , you will have access to all the fields inside it, for example, TempData .

However, if you use OWIN , then you can probably use Controller.User , which has an extension method, to get Id and properties to get most other standard data, such as Name and others, to get the user ID.

+6
source

I believe the ActionExecutingContext contains a link to the calling controller. Using this mixed with a custom controller class derived from the Controller base class to then store id as a controller instance variable is likely to do this.

User controller

 Public Class MyController : Controller { Public int Id {get;set;} } 

LoginFilter

 public class LoginFilter : ActionFilterAttribute { public override void OnActionExecuting(ActionExecutingContext filterContext) { // Authenticate (somehow) and retrieve the ID int id = Authentication.SomeMethod(); ((MyController)filterContext.Controller).Id = id; //Assign the Id by casting the controller (you might want to add a if ... is MyController before casting) } } 

controller

 [LoginFilter] public class Dashboard : MyController { public ActionResult Index() { //Read the Id here int id = this.Id } } 
+4
source

All Articles