当你创建一个新的asp.net项目ivisvis studio 2012时,它会用这段代码添加一个ascx:`//确定当前视图 var isMobile = WebFormsFriendlyUrlResolver.IsMobileView(new HttpContextWrapper(Context)); CurrentView = isMobile? “手机”:“桌面”;
// Determine alternate view
AlternateView = isMobile ? "Desktop" : "Mobile";
// Create switch URL from the route, e.g. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page
var switchViewRouteName = "AspNet.FriendlyUrls.SwitchView";
var switchViewRoute = RouteTable.Routes[switchViewRouteName];
if (switchViewRoute == null)
{
// Friendly URLs is not enabled or the name of the swith view route is out of sync
this.Visible = false;
return;
}
var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView });
url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
SwitchUrl = url;`
我真的不理解它,它是如何工作的?这个奇怪的代码是什么?WebFormsFriendlyUrlResolver?我有一个现有的项目,我想知道如果检测到移动浏览器是否可以切换母版页
答案 0 :(得分:6)
WebFormsFriendlyUrlResolver是一个获取路由关联的辅助类。如果您想启用友好网址,请使用,例如www.yourdomain.com/myaccount.aspx可以显示为www.yourdomain.com/Account
你不需要使用它(针对你的特定问题),但它是asp.net的一个很酷的功能,并且在RouteTables中创建自定义路径变得容易
Scott的this article帮助我理解友好的URL
现在针对您的问题,更改移动设备的母版页,母版页只能在页面的pre-init事件中更改。我之后不知道另一种注入新母版页的方法,因为我认为为时已晚
当你有很多页面时,挂钩handler to httpcontext 下面是一个伪代码,需要根据您的需要进行改进
void page_PreInit(object sender, EventArgs e)
{
Page p = this.Context.Handler as Page;
if (p != null)
{
// set master page
if(Request.Browser.IsMobileDevice){
p.MasterPageFile = "~/MasterPages/mobile.master";
}
else{
p.MasterPageFile = "~/MasterPages/normal.master";
}
}
}
确定后,请务必阅读此解决方案at SO,其中提到为移动设备构建母版页
祝你好运答案 1 :(得分:1)
要包含在移动版和桌面版之间切换的代码,您必须包含在页面的边缘:
<%@ Register Src="~/ViewSwitcher.ascx" TagPrefix="friendlyUrls" TagName="ViewSwitcher" %>
然后在您的页面中包含View Switcher Control:
<friendlyUrls:ViewSwitcher ID="ViewSwitcher1" runat="server" />
此控件会自动将页面的母版页从 Site.Master 切换为 Site.Mobile.Master
不要忘记在 ViewSitcher.ascx.vb 上完成 Page_Load 。这是代码:
Protected Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load
' Determine current view
Dim isMobile = WebFormsFriendlyUrlResolver.IsMobileView(New HttpContextWrapper(Context))
CurrentView = If(isMobile, "Mobile", "Desktop")
' Determine alternate view
AlternateView = If(isMobile, "Desktop", "Mobile")
Dim strSwitchViewRouteName As String = "AspNet.FriendlyUrls.SwitchView"
Dim SwitchViewRoute = RouteTable.Routes(strSwitchViewRouteName)
If SwitchViewRoute Is Nothing Then
' Friendly URLs is not enabled or the name of the switch view route is out of sync
Me.Visible = False
Return
End If
' Create switch URL from the route, e.g. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page
Dim url = GetRouteUrl(strSwitchViewRouteName, New With { _
Key .view = AlternateView, .__FriendlyUrls_SwitchViews = True _
})
url += "?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl)
SwitchUrl = url
End Sub
最后一个注意事项...... ViewSwitcher 会自动更改me.MasterPageFile。例如,如果您的表单使用MasterPage “〜/ MyMasterPage.Master”,则库搜索“〜/ MyMasterPage.Mobile.Master”,如果文件存在,则库自动替换您的表单母版页:“〜/ MyMasterPage.Mobile.Master”
请注意,您必须使用MasterPageFile的绝对地址。这是有效的:“〜/ MyMasterpage.Master”,这将显示从库生成的错误消息:“MyMasterPage.Master”
<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/MyMasterpage.Master" AutoEventWireup="true" CodeBehind="Default.aspx.vb" nherits="Test._Default" %>