Accessing Action Filter's data in Controller Action
[ApiBasicAuthorize]
public ActionResult SignIn()
{
}
I have this custom filter called ApiBasicAuthorize. Is it 开发者_JAVA百科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?
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();
}
精彩评论