在MVC 3动态变化ViewStart布局路径 [英] Dynamic change ViewStart layout path in MVC 3
问题描述
在我的MVC项目有2个领域是管理员和客户端,我需要动态配置布局客户端,在_ViewStart(客户端)文件将设置布局所有客户端的页面。
In my MVC project has 2 Areas which is Admin and Client and I need to dynamic config Layout for Client side, In _ViewStart (in client) file will set layout for all of client page.
Layout = "~/Views/Shared/_Layout.cshtml";
所以,如果我们需要改变客户端的布局,我们可以改变_ViewStart CSHTML文件的布局文件的路径对不对?我找不到如何ViewStart文件中更改或是否有在这种情况下,另一种解决方案。
So if we need to change client layout we can change Layout path of cshtml file in _ViewStart file right? I cant find how to change inside ViewStart file or Is there another solution in this case.
感谢您的帮助:)
推荐答案
记住内的任何的 @ {...}
被视为 $ C $ ç。因此,应该在那里放置一个条件一个简单的事情来改变它是如何继承:
Remember that anything within the @{ ... }
is treated as code. So, it should be a simple matter of placing a condition in there to change how it's inherited:
@{
Layout = "~/Views/Shared/_Layout.cshtml";
if (User.Current.IsAuthenticated) {
Layout = "~/Views/Shared/_AdminLayout.cshtml";
}
}
虽然你probaby最好还是考虑一下主旋律(并有管理员/用户的主题)。或者,你可以让你的 _Layout.cshtml
更聪明,有它处理根据条件以及不同的看法。
Though you're probaby better off looking at Themes (and have an admin/user theme). Alternatively, you can make your _Layout.cshtml
smarter and have it handle the different views based on conditions as well.
另请参见:<一href=\"http://stackoverflow.com/questions/4303948/mvc3-razor-is-there-a-way-to-change-the-layout-depending-on-browser-request\">MVC3剃须刀 - ?有没有一种方法依赖于浏览器请求更改布局
这篇关于在MVC 3动态变化ViewStart布局路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!