有人可以帮我干这些ASP.NET MVC控制器ActionResults

时间:2011-10-13 01:15:28

标签: asp.net asp.net-mvc dry

抓取当前用户的OU并返回一个自定义模型(Division),该模型具有一些关于OU的属性。然后通过ViewBag传递它。我的控制器看起来像这样:

public ActionResult NewAccountRequest()
{
        var currentUser = HttpContext.User;
        var userDivision = ADTools.GetDivisionFromUser(currentUser.Identity.Name);
        ViewBag.UserDivision = userDivision;
        ....
}

[HttpPost]
public ActionResult LookupUNI(string uni)
{
        var currentUser = HttpContext.User;
        var userDivision = ADTools.GetDivisionFromUser(currentUser.Identity.Name);
        ViewBag.UserDivision = userDivision;
        ....
}

1 个答案:

答案 0 :(得分:2)

如果这对您的所有操作都很常见,您可以将其添加到控制器的OnActionExecuted中。

public void override OnActionExecuted( ActionExecutedContext context )
{
     if (context.Result is ViewResult)
     {
         var currentUser = HttpContext.User;
         var userDivision = ADTools.GetDivisionFromUser(currentUser.Identity.Name);
         ViewBag.UserDivision = userDivision;
     }
}