[ApiBasicAuthorize]
public ActionResult SignIn()
{
}
I have this custom filter called ApiBasicAuthorize. Is it possible to access ApiBasicAuthorize's data (properties etc) inside the controller action SignIn?
If not, how do I pass data from the filter to controller action?
An action (or action method ) is a method on a controller that handles incoming requests. Controllers provide a logical means of grouping similar actions together, allowing common sets of rules (e.g. routing, caching, authorization) to be applied collectively. Incoming requests are mapped to actions through routing.
ViewData, ViewBag, and TempData are used to pass data between controller, action, and views. To pass data from the controller to view, either ViewData or ViewBag can be used. To pass data from one controller to another controller, TempData can be used.
ASP.NET MVC provides Action Filters for executing filtering logic either before or after an action method is called. Action Filters are custom attributes that provide declarative means to add pre-action and post-action behavior to the controller's action methods.
There is a dictionary called items attached to the HttpContext object. Use this dictionary to store items shared across components during a request.
public override void OnAuthorization(AuthorizationContext filterContext)
{
filterContext.HttpContext.Items["key"] = "Save it for later";
base.OnAuthorization(filterContext);
}
Then anywhere in your code later in the request...
var value = HttpContext.Current.Items["key"];
public override void OnAuthorization(AuthorizationContext filterContext)
{
var rd = filterContext.RouteData;
//add data to route
rd.Values["key"]="Hello";
base.OnAuthorization(filterContext);
}
public ActionResult(string key)
{
//key= Hello
return View();
}
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