【发布时间】:2014-02-23 08:18:36
【问题描述】:
我想在 Global.asax 中调用控制器方法。代码如下。
protected void Application_PostAuthenticateRequest(Object sender, EventArgs e)
{
if (User.Identity.IsAuthenticated)
{
//here we can subscribe user to a role via Roles.AddUserToRole()
}
}
此事件在 global.asax 中。我想调用从数据库返回用户权限的控制器方法。在我将用户权限保存在会话和我的控制器构造函数中之后,这怎么可能在这里调用控制器方法?代码如下。
public class AccountController : Controller
{
private readonly ISecurityService securityService;
public AccountController(ISecurityService securityService)
{
this.securityService = securityService;
}
}
请指导我。
【问题讨论】:
-
我会将用户权限分离到一个单独的类中,并让控制器和 global.asax 调用该类。我会避免你的 global.asax 和你的控制器之间有任何依赖关系,因为你最终可能会陷入无限循环。
-
请你给我任何例子@Rob Lang 吗?
标签: c# asp.net asp.net-mvc asp.net-mvc-4 asp.net-mvc-5