使用 Html.Action 时,ControllerContext 为 null 且 BaseController.OnActionExecuting() 未调用
我们使用 BaseController
在每个操作执行之前缓存基本身份验证信息:
public abstract class BaseController : Controller
{
protected bool IsLoggedIn { get; set; }
protected string Username { get; set; }
...
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var identity = base.User.Identity;
this.IsLoggedIn = identity.IsAuthenticated;
this.Username = identity.Name;
...
}
}
并且我们的子控制器具有主页(Index)和部分视图(GetNavigation)的操作:
[Authorize]
public partial class CollaborationController : BaseController
{
[HttpGet]
public virtual ViewResult Index()
{
var viewModel = this.MakeViewModel<FullPageViewModel>();
return this.View(MVC.Collaboration.Views.Index, viewModel);
}
[HttpGet]
public virtual PartialViewResult GetNavigation()
{
var viewModel = NavigationViewModel.Make(this.User);
return this.PartialView(MVC.Collaboration.Views.Navigation, viewModel);
}
}
并且部分视图直接渲染with Html.Action()
:
@Html.Action(MVC.Collaboration.GetNavigation())
看起来应该可以工作,但是 BaseController.OnActionExecuting
不会被调用。我什至无法直接调用它,因为 this.ControllerContext
和 base.User
都是 null
。我还尝试对 ActionFilterAttribute
进行子类化,但它的 OnActionExecuting 方法也没有被调用。
We use a BaseController
to cache basic authentication information before every action executes:
public abstract class BaseController : Controller
{
protected bool IsLoggedIn { get; set; }
protected string Username { get; set; }
...
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
var identity = base.User.Identity;
this.IsLoggedIn = identity.IsAuthenticated;
this.Username = identity.Name;
...
}
}
And our child controller has a actions for the main page (Index) and a partial view (GetNavigation):
[Authorize]
public partial class CollaborationController : BaseController
{
[HttpGet]
public virtual ViewResult Index()
{
var viewModel = this.MakeViewModel<FullPageViewModel>();
return this.View(MVC.Collaboration.Views.Index, viewModel);
}
[HttpGet]
public virtual PartialViewResult GetNavigation()
{
var viewModel = NavigationViewModel.Make(this.User);
return this.PartialView(MVC.Collaboration.Views.Navigation, viewModel);
}
}
And the partial view is rendered directly with Html.Action()
:
@Html.Action(MVC.Collaboration.GetNavigation())
Seems like it should work, but BaseController.OnActionExecuting
does not get called. And I can't even call it directly because this.ControllerContext
and base.User
are both null
. I also tried subclassing ActionFilterAttribute
, but its OnActionExecuting method doesn't get called, either.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我知道这是一个老问题,但这是我处理这个问题的方法。在我的子控制器中,我创建 OnActionExecuting 方法并从那里调用基本控制器。
I know this is an old question but here is how I handle this. In my child controller I create the OnActionExecuting method and call the base controller from there.
至少有点回答了我自己的问题:
替换
修复
它。 MVCContrib 的语法似乎是罪魁祸首,有人知道为什么吗?更好的是,有人知道一种解决方法可以让我避免那些讨厌的、非重构安全的魔法字符串吗?
At least sort-of answered my own question:
Substituting
for
fixes it. MVCContrib's syntax seems to be the culprit, anyone know why? Even better, anyone know a work-around that lets me avoid those nasty, non-refactoring-safe, magic strings?