使用 Html.Action 时,ControllerContext 为 null 且 BaseController.OnActionExecuting() 未调用

发布于 2024-12-04 18:46:37 字数 1434 浏览 0 评论 0原文

我们使用 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.ControllerContextbase.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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

笨死的猪 2024-12-11 18:46:37

我知道这是一个老问题,但这是我处理这个问题的方法。在我的子控制器中,我创建 OnActionExecuting 方法并从那里调用基本控制器。

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
     base.OnActionExecuting(filterContext);
}

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.

protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
     base.OnActionExecuting(filterContext);
}
⒈起吃苦の倖褔 2024-12-11 18:46:37

至少有点回答了我自己的问题:

替换

@Html.Action("GetNavigation", "Collaboration")

修复

@Html.Action(MVC.Collaboration.GetNavigation())

它。 MVCContrib 的语法似乎是罪魁祸首,有人知道为什么吗?更好的是,有人知道一种解决方法可以让我避免那些讨厌的、非重构安全的魔法字符串吗?

At least sort-of answered my own question:

Substituting

@Html.Action("GetNavigation", "Collaboration")

for

@Html.Action(MVC.Collaboration.GetNavigation())

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文