4

当您在 Visual Studio 2012 中创建一个新的 asp.net 项目时,它会使用以下代码添加一个 ascx:

// Determine current view

var isMobile = WebFormsFriendlyUrlResolver.IsMobileView(new HttpContextWrapper(Context));
CurrentView = isMobile ? "Mobile" : "Desktop";

// 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? 我有一个现有的项目,我想知道如果检测到移动浏览器是否可以切换母版页?

4

2 回答 2

6

WebFormsFriendlyUrlResolver是获取路由关联的辅助类。如果您想启用友好的网址,可以使用它,即www.yourdomain.com/myaccount.aspx可以显示为www.yourdomain.com/Account

您不需要使用它(针对您的特定问题),但是它是 asp.net 的一个很酷的功能,并且通过在 RouteTables 中创建自定义路由变得容易

Scott 的这篇文章帮助我理解了友好的 URL

现在解决您的问题,更改移动设备的母版页。母版页只能在页面的预初始化事件中更改。我不知道在那之后注入新母版页的另一种方法,因为我认为为时已晚

当您有很多页面时,将此处理程序挂钩到 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";
            }

        }
    } 

弄清楚这一点后,请确保您在 SO阅读此解决方案,其中提到为移动设备构建母版页

于 2013-05-29T11:17:21.143 回答
1

要包含在移动版本和桌面版本之间切换的代码,您必须在页面开头包含:

<%@ Register Src="~/ViewSwitcher.ascx" TagPrefix="friendlyUrls" TagName="ViewSwitcher" %>

然后在您的页面中包含 View Switcher Control:

<friendlyUrls:ViewSwitcher ID="ViewSwitcher1" runat="server" />

此控件自动将页面的母版页Site.MasterSite.Mobile.Master

并且不要忘记完成Page_LoadViewSwitcher.ascx.vb这是代码:

 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。例如,如果您的表单使用"~/MyMasterPage.Master"库搜索 的 MasterPage "~/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" %>
于 2015-02-17T14:37:13.657 回答