在渲染时动态选择 Spark Master 布局

发布于 2025-01-01 22:59:32 字数 703 浏览 3 评论 0原文

每次发出页面请求时,我都需要选择 Spark Master 布局。我尝试通过在 OnActionExecuting 中设置 ViewBag.Layout 值并在主布局引用中引用该值来实现此目的。

<use master="${ViewBag.Layout}"/>

但是,这不起作用,似乎 Spark 没有将括号内的代码视为代码,而是视为字符串。我收到以下错误:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Layouts\${ViewBag.Layout}.spark
Shared\${ViewBag.Layout}.spark

任何人都可以告诉我这是为什么吗?或者指出我有另一种方法可以做到这一点?

I need to choose my spark master layout every time a request for a page is made. I attempted to so this by setting a ViewBag.Layout value in OnActionExecutingand referencing this value in the master layout ref.

<use master="${ViewBag.Layout}"/>

However, this doesn't work, it seems as if spark is not treating the code within the brackets not as code, but as a string. I get the following error:

The view 'Index' or its master was not found or no view engine supports the searched locations. The following locations were searched:
~/Views/Home/Index.aspx
~/Views/Home/Index.ascx
~/Views/Shared/Index.aspx
~/Views/Shared/Index.ascx
~/Views/Home/Index.cshtml
~/Views/Home/Index.vbhtml
~/Views/Shared/Index.cshtml
~/Views/Shared/Index.vbhtml
Layouts\${ViewBag.Layout}.spark
Shared\${ViewBag.Layout}.spark

Can anyone tell me why this is? Or point me to an alternative way of doing this?

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

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

发布评论

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

评论(2

巷雨优美回忆 2025-01-08 22:59:32

无法使用代码语法动态选择布局。其原因是该布局是在引擎中进行任何渲染之前选择的。因此,首先定位布局,然后引擎尝试将所有变量渲染到位。对布局使用变量意味着渲染引擎不知道要打开哪个文件。

The layout cannot be dynamically selected using code syntax. The reason for this is that that layout is selected prior to any rendering taking place in the engine. So first the layout is located, and then the engine tries to render all the variables in place. Using a variable for the layout means that the rendering engine doesn't know which file to open.

梦里梦着梦中梦 2025-01-08 22:59:32

实际上。这是可能的..而不是使用代码示例。使用结果过滤器。

public void OnResultExecuting(ResultExecutingContext filterContext) {
        var viewResult = filterContext.Result as ViewResult;
        if (viewResult == null)
            return;

        var layoutFile = viewResult.ViewBag.Layout; //the variable you set in your action executing,

        viewResult.MasterName = layoutFile;

    }

Actually. it is possible.. Instead of using the code example. Use a ResultFilter.

public void OnResultExecuting(ResultExecutingContext filterContext) {
        var viewResult = filterContext.Result as ViewResult;
        if (viewResult == null)
            return;

        var layoutFile = viewResult.ViewBag.Layout; //the variable you set in your action executing,

        viewResult.MasterName = layoutFile;

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