从 OnActionExecuting 显式设置时 ViewModel 为 null

发布于 2024-11-24 13:15:34 字数 1187 浏览 0 评论 0原文

我研究了一些关于SO的问题,但似乎找不到答案。谁知道呢,也许我正在打破最佳实践。在阅读了关于使用布局注入器属性能够通过简单地指定属性来设置布局的优秀答案之后,我想我也可以使用这种类来对需要用户进行身份验证的属性控制器进行属性化。

因此,我不必在各处检查身份验证,而是编写了以下内容:

    // class AdminAttribute
public class AdminAttribute : LayoutInjectorAttribute
{
    // ctor
    public AdminAttribute()
        : base("_LayoutAdmin")
    {
    }   // eo ctor


    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        WebUser user = WebApplication.CurrentUser;
        if (!user.IsAuthenticated)
        {
            filterContext.Result = new ViewResult() { ViewName = "Unauthorised" };
            filterContext.Controller.ViewData.Model = new ViewModel(WebApplication.CurrentUser.Translate("msgunauthorisedtitle"));
            filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
            return;
        }
        base.OnActionExecuting(filterContext);
    }   // eo OnActionExecuting

}   // eo class AdminAttribute

一切正常,除了处理视图时模型为 null 的事实之外。有谁知道为什么会这样?

查看页面(Unauthorized.cshtml)

@model Py2.Web.ViewModel
<h2>@Model.Title</h2>

提前致谢!

I researched a few questions on SO, but couldn't seem to locate the answer to this. Who knows, maybe I am breaking best-practice. After reading an excellent answer on using a layout injector attribute to be able to set the layout by simply specifying an attribute, I figured I could also use this kind of class to attribute controllers which require the user to be authenticated.

So, rather than me having to check authentication everywhere, I wrote the following:

    // class AdminAttribute
public class AdminAttribute : LayoutInjectorAttribute
{
    // ctor
    public AdminAttribute()
        : base("_LayoutAdmin")
    {
    }   // eo ctor


    public override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        WebUser user = WebApplication.CurrentUser;
        if (!user.IsAuthenticated)
        {
            filterContext.Result = new ViewResult() { ViewName = "Unauthorised" };
            filterContext.Controller.ViewData.Model = new ViewModel(WebApplication.CurrentUser.Translate("msgunauthorisedtitle"));
            filterContext.Result.ExecuteResult(filterContext.Controller.ControllerContext);
            return;
        }
        base.OnActionExecuting(filterContext);
    }   // eo OnActionExecuting

}   // eo class AdminAttribute

All works, aside from the fact that the model is null when processing the view. Does anyone have an idea why this may be?

The view page (Unauthorised.cshtml)

@model Py2.Web.ViewModel
<h2>@Model.Title</h2>

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

糖果控 2024-12-01 13:15:34

请像这样尝试:

if (!user.IsAuthenticated)
{
    var result = new ViewResult { ViewName = "Unauthorised" };
    var model = new ViewModel(
        WebApplication.CurrentUser.Translate("msgunauthorisedtitle")
    );
    result.ViewData.Model = model;
    filterContext.Result = result;
    return;
}

请注意,无需显式调用 ExecuteResult,这是 ASP.NET MVC 框架的工作,只需通过向 ViewResult 传递一个模型 (result.ViewData.模型=模型)。另请注意此视图模型传递到视图结果的方式,而不是像您在代码中所做的那样传递到 filterContext.Controller.ViewData.Model

Try like this:

if (!user.IsAuthenticated)
{
    var result = new ViewResult { ViewName = "Unauthorised" };
    var model = new ViewModel(
        WebApplication.CurrentUser.Translate("msgunauthorisedtitle")
    );
    result.ViewData.Model = model;
    filterContext.Result = result;
    return;
}

Notice that there is no need to call ExecuteResult explicitly, that's the job of the ASP.NET MVC frameowrk, simply return the ViewResult by passing it a model (result.ViewData.Model = model). Also notice the way this view model is passed to the view result and not to filterContext.Controller.ViewData.Model as you were doing in your code.

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