关闭ASP.NET友好URL移动主页。

20
我想完全关闭 Site.Mobile.Master 页面。我的网站是响应式的,我希望移动浏览器使用相同的母版页。
在 ASP.NET 友好 URL 中如何实现?
谢谢。
8个回答

16

删除Site.Mobile.Master页面,友好的URL将使用常规的Site.Master页面。


截至2021年3月,这个答案并不适用于所有情况。请参考MikeBaz的答案以获得更干净的解决方案。 - Brian Webster

15
当前Web Forms友好的URL(1.0.2)版本中似乎存在一个 bug,导致尝试访问site.mobile.master 时在友好的URL代码中出现了 "The relative virtual path 'Site.Mobile.Master' is not allowed here." 的错误。我就是因为这个问题而受挫了。
为了解决这个问题,我使用了修改版的代码,链接在http://www.davidwilhelmsson.com/disabling-mobile-master-pages-with-asp-net-friendly-urls/ ,首先创建了一个解析器类:
/// <summary>
/// This is a hack to force no mobile URL resolution in FriendlyUrls.  There's some kind of bug in the current version that
/// causes it to do an internal failed resolve of a mobile master even though there is none.
/// </summary>
public class BugFixFriendlyUrlResolver: Microsoft.AspNet.FriendlyUrls.Resolvers.WebFormsFriendlyUrlResolver {
    protected override bool TrySetMobileMasterPage(HttpContextBase httpContext, Page page, string mobileSuffix) {
        return false;
        //return base.TrySetMobileMasterPage(httpContext, page, mobileSuffix);
    }
}

然后我在我的RouteConfig类中使用它:

    public static void RegisterRoutes(RouteCollection routes) {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings, new BugFixFriendlyUrlResolver());
    }

不太确定我是否理解了这个问题。Resolver类只是项目中的一个类,并在标准的RouteConfig中注册在“App_Start”中,如所示。 RouteConfig是从Global.asax.cs调用的。 - MikeBaz - MSFT
我最终把它放在了App_Start文件夹中。 - MC9000
那是唯一的解决方案,我可以真正删除Site.Mobile.Master。谢谢! - Andreas Krohn

7

很奇怪的是,没有简单的方法可以摆脱移动主页面。如果我删除Site.Mobile.Master.master文件,就会出现错误:“文件'/Site.Mobile.Master'不存在”。

为了解决这个问题,我添加了以下代码到Site.Mobile.Master.cs Page_Load事件中。

var AlternateView = "Desktop";
var switchViewRouteName = "AspNet.FriendlyUrls.SwitchView";
var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView, __FriendlyUrls_SwitchViews = true });
url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
Response.Redirect(url);

2
我成功解决了这个问题,方法如下:
1. 删除 mobile.master 文件。
2. 更改 ViewSwitcher.ascx.cs 中的代码如下:
protected void Page_Load(object sender, EventArgs e)
    {
        CurrentView = "Desktop";
        AlternateView = "Desktop";

        // 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 switch view route is out of sync
            this.Visible = false;
            return;
        }
        var url = GetRouteUrl(switchViewRouteName, new { view = AlternateView, __FriendlyUrls_SwitchViews = true });
        url += "?ReturnUrl=" + HttpUtility.UrlEncode(Request.RawUrl);
        SwitchUrl = url;

    }

3) 直到我删除整个目录并重新发布它时,这才起作用。我认为,删除一些特定的文件可能也有帮助。但是由于我没有那样的环境,所以我选择了更简单的方式。


2
当我删除Site.Mobil.Master时,页面出现了问题。 因此... 我更喜欢在Site.Mobile.Master中设置Site.Master的信息。
CodeBehind="Site.Master.cs" Inherits="App.SiteMaster"
这不是最好的选择(哈哈),但解决了问题!

这是唯一对我起作用的方法。太荒谬了。我用我的Site.Master文件替换了Site.Mobile.Master中的所有内容,除了第一行(<%@ Master..... %>),我按照上面的建议进行了更新。 - Marcus

0

我发现直接删除(或重命名)Site.Mobile.Master和ViewSwitcher.ascx更容易。这对我来说似乎完全有效。


0

在默认的VS Web项目中,我找不到TrySetMobileMasterPage()。在VS2013中是否有不同的方法? - MC9000
我在 Microsoft.AspNet.FriendlyUrls.dll 中找到了它。 我想微软不会提供那个源代码(实际上你也不需要它)。 - MC9000

0
我通过在“ViewSwitcher.ascx”的“Page_Load”事件末尾添加默认保存的重定向来解决了这个问题: Response.Redirect(url) 因此,该子程序的结果为:

Protected Sub Page_Load(sender As Object, e As EventArgs) ' Determinar la vista actual Dim isMobile = WebFormsFriendlyUrlResolver.IsMobileView(New HttpContextWrapper(Context)) CurrentView = If(isMobile, "Mobile", "Desktop")

    ' Determinar la vista alternativa
    AlternateView = If(isMobile, "Desktop", "Mobile")

    ' Create URL de conmutador a partir de la ruta, p. ej. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page
    Dim switchViewRouteName = "AspNet.FriendlyUrls.SwitchView"
    Dim switchViewRoute = RouteTable.Routes(switchViewRouteName)
    If switchViewRoute Is Nothing Then
        ' Las URL descriptivas no están habilitadas o el nombre de la ruta de la vista del conmutador no está sincronizado
        Me.Visible = False
        Return
    End If
    Dim url = GetRouteUrl(switchViewRouteName, New With {
        .view = AlternateView,
        .__FriendlyUrls_SwitchViews = True
    })
    url += "?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl)
    SwitchUrl = url
    **Response.Redirect(url)**
End Sub

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接