【发布时间】:2016-08-09 13:34:16
【问题描述】:
采用简单的操作过滤器,检查用户是否登录并检索其用户 ID。
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?
.....
}
}
然后我怎样才能将此 ID 传递给我的控制器操作?
[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 等效的工具可以让我这样做?或者其他一些允许我在过滤器和控制器操作之间传递变量和对象的技术?
【问题讨论】:
标签: c# asp.net asp.net-mvc action-filter actionfilterattribute