简体   繁体   English

在OnActionExecuted中定义视图的布局

[英]Defining Layout for the View in OnActionExecuted

In an MVC 5.1 web application project, I have a controller that is dedicated to the Facebook Canvas Application of the project. 在MVC 5.1 Web应用程序项目中,我有一个专用于该项目的Facebook Canvas Application的控制器。 I need all the Views returned by the controller to have a specific Layout. 我需要控制器返回的所有视图都具有特定的布局。 (Normally, I would not define the Layout property in the controller for it would have too much to say on the View, but in this case I'm willing to let the controller dictate this since it's all about the facebook canvas). (通常,我不会在控制器中定义Layout属性,因为它在View上的内容太多了,但是在这种情况下,我愿意让控制器来指定它,因为这全都与facebook画布有关)。

I can achieve this by returning 我可以通过返回来实现

return View("View", "~/Views/Shared/_FacbookCanvasLayout.cshtml");

but in that case I'd have to use this for every single view. 但在那种情况下,我将不得不在每个视图中使用它。 What I want instead is to override the OnActionExecuted method in a base controller class and define the layout there. 相反,我要覆盖的是基本控制器类中的OnActionExecuted方法,并在那里定义布局。

So my question is, how can I define the layout for the view in the OnActionExecuted class - or if there is a better solution for this, how can I do that? 所以我的问题是,如何在OnActionExecuted类中定义视图的布局-或如果对此有更好的解决方案,该怎么办?

How about doing it this way then ? 那么怎么做呢?

    protected override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        base.OnActionExecuted(filterContext);
        var res = filterContext.Result as ViewResult;
        if (res != null)
            res.MasterName = "~/Views/Shared/_FacbookCanvasLayout.cshtml";
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM