剃刀嵌套式布局与层叠节 [英] Razor Nested Layouts with Cascading Sections

查看:104
本文介绍了剃刀嵌套式布局与层叠节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用剃须刀作为其视图引擎的MVC3站点。我希望我的网站是换肤功能。最可能的皮肤非常相似,以至于他们可以从一个共享的总体布局中获得。

I have an MVC3 site using Razor as its view engine. I want my site to be skinnable. Most of the possible skins are similar enough that they can derive from a shared master layout.

所以,我正在考虑这样的设计:

Therefore, I am considering this design:

不过,我希望能够调用 RenderSection 在底层, _Common.cshtml ,和有它呈现在顶层限定的部分中, Detail.cshtml 。这不起作用: RenderSection 显然只呈现被定义下一层章节的内容。

However, I would like to be able to call RenderSection in the bottom layer, _Common.cshtml, and have it render a section that is defined in the top layer, Detail.cshtml. This doesn't work: RenderSection apparently only renders sections that are defined the next layer up.

当然,我可以定义每个皮肤每个部分。例如,如果 _Common 需要调用 RenderSection(HD)详细信息,我只是把这个在每个 _Skin 和它的作品:

Of course, I can define each section in each skin. For instance, if _Common needs to call RenderSection("hd") for a section defined in Detail, I just place this in each _Skin and it works:

@section hd {
    @RenderSection("hd")
}

这导致code上有一些重复(因为每个皮肤现在必须有同样的部分),一般感觉凌乱。我还是新剃刀,它看起来像我可能会丢失一些东西明显。

This results in some duplication of code (since each skin must now have this same section) and generally feels messy. I'm still new to Razor, and it seems like I might be missing something obvious.

在调试时,我可以看到WebViewPage.SectionWritersStack定义部分的完整列表。如果我能告诉RenderSection通过整个列表放弃前一看,会发现我所需要的部分。唉,SectionWritersStack是不公开的。

When debugging, I can see the complete list of defined sections in WebViewPage.SectionWritersStack. If I could just tell RenderSection to look through the entire list before giving up, it would find the section I need. Alas, SectionWritersStack is non-public.

另外,如果我可以访问布局页的RenderSection的层次,并试图执行在每个不同的情况下,我能找到我所需要的部分。我可能失去了一些东西,但我看不出有什么办法做到这一点。

Alternatively, if I could access the hierarchy of layout pages and attempt execution of RenderSection in each different context, I could locate the section I need. I'm probably missing something, but I don't see any way to do this.

是否有某种方式来实现这一目标,除了我已经介绍的方法之外?

Is there some way to accomplish this goal, other than the method I've already outlined?

推荐答案

这其实是在今天使用公共API(不是使用部分重新定义的方法等)是不可能的。你可能有一些运气使用私有反映,那当然是一个脆弱的做法。我们将考虑在剃刀的下一个版本使这种情况更加容易。

This is in fact not possible today using the public API (other than using the section redefinition approach). You might have some luck using private reflection but that of course is a fragile approach. We will look into making this scenario easier in the next version of Razor.

在此期间,这里的一对夫妇的博客文章我写关于这个问题:

In the meantime, here's a couple of blog posts I've written on the subject:

  • http://blogs.msdn.com/b/marcinon/archive/2010/12/08/optional-razor-sections-with-default-content.aspx
  • http://blogs.msdn.com/b/marcinon/archive/2010/12/15/razor-nested-layouts-and-redefined-sections.aspx

这篇关于剃刀嵌套式布局与层叠节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆