问题描述
我跟着这个教程成功:http://www.hanselman.com/blog/MixMobileWebSitesWithASPNETMVCAndTheMobileBrowserDefinitionFile.aspx
当我访问该网页与移动设备的所有视图呈现成功。但是,他们呈现与错误的布局(AKA母版)。
All view are rendered with success when I access the page with mobile device. But, they are rendered with wrong layout (AKA masterpage).
我有以下结构:
/Views/Shared/Mobile/_Layout.cshtml
/Views/Shared/_Layout.cshtml
I have the following structure:/Views/Shared/Mobile/_Layout.cshtml/Views/Shared/_Layout.cshtml
问题是,我必须把下面的语句在每个视图:
The problem is, I have to put the following statement in EVERY view:
Layout = "~/Views/Shared/Mobile/_Layout.cshtml";
有没有在那里我可以把我的逻辑来呈现另一个一一布局的地方?
Is there a place where I can place my logic to render one layout on another one?
如果(normalAccess)呈现正常_Layout.cshtml
其他(MobileAccess公司)渲染/Mobile/_Layout.cshtml
if (normalAccess) render normal _Layout.cshtmlelse (mobileAccess) render /Mobile/_Layout.cshtml
我找不到在哪里。
感谢您的帮助。
推荐答案
有一个善于http://weblogs.asp.net/scottgu/archive/2010/10/22/asp-net-mvc-3-layouts.aspx
您可以创建显然在\\ Views文件夹的文件名为_ViewStart.cshtml在那里你可以把你的布局逻辑由所有视图中使用
You can apparently create a file in your \Views folder called _ViewStart.cshtml where you can put your layout logic to be used by all views
样品_ViewStart.cshtml很简单:
The sample _ViewStart.cshtml is simply:
@{
Layout = "~/Views/Shared/SiteLayout.cshtml";
}
该文章还指出:因为_ViewStart.cshtml允许我们编写code,我们可以选择性地使我们的布局选择逻辑不仅仅是一个基本的属性集丰富。例如:我们可以改变我们使用取决于什么类型的设备访问该网站的布局模板 - 并有一个电话或这些设备的平板电脑优化的布局,并为PC /笔记本电脑桌面优化布局
The article also states: 'Because the _ViewStart.cshtml allows us to write code, we can optionally make our Layout selection logic richer than just a basic property set. For example: we could vary the Layout template that we use depending on what type of device is accessing the site – and have a phone or tablet optimized layout for those devices, and a desktop optimized layout for PCs/Laptops.'
这可能需要你一些玩弄得到这个工作,我没有安装2010然而得心应手试试这个。
It might take you some playing around with to get this working, I don't have a 2010 install handy to try this however.
这篇关于MVC3剃须刀 - 有没有办法来改变依赖于浏览器请求的布局?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!