ASP.NET turn off FriendlyURLs mobile.master page

核能气质少年 提交于 2019-12-18 03:14:28

问题


I would like to turn the Site.Mobile.Master page off completely. My site is responsive and i want mobile browsers to use the same master page.

How can i do this in ASP.NET friendly URLs

Thanks


回答1:


Delete the Site.Mobile.Master page, and Friendly URLs will just use the regular Site.Master page instead.




回答2:


There actually seems to be a bug in the current version of Web Forms friendly URLs (1.0.2) that makes this attempted access to the site.mobile.master break with "The relative virtual path 'Site.Mobile.Master' is not allowed here." in the friendly URL code. I just was burned by this.

To fix it, I used a modified version of the code at http://www.davidwilhelmsson.com/disabling-mobile-master-pages-with-asp-net-friendly-urls/ - first made a resolver class:

/// <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);
    }
}

then used it in my RouteConfig class:

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



回答3:


It is relly weird that there is no easy way to get rid of mobile master page. If I delete Site.Mobile.Master.master, I ended with the error "The file '/Site.Mobile.Master' does not exists".

What I did to solve this issue was I added following codes into Site.Mobile.Master.cs Page_Load event.

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);



回答4:


When I deleted Site.Mobil.Master the pages was broken. So... I prefer just set in Site.Mobile.Master set the info of Site.Master

CodeBehind="Site.Master.cs" Inherits="App.SiteMaster"

its not the best option(LOL), but Solved!




回答5:


I managed to solve this problem, by changing following:

1) I deleted mobile.master

2) I changed code at ViewSwitcher.ascx.cs to folowing

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) This didn't work, until I deleted whole directory and republished it. I think, deleting some specific files might help as well. But since I didn't have that kind of environment, then I went easier way.




回答6:


Here is an article I have written to fix this issue. Please refer.

http://www.icodefor.net/2015/06/fixes-for-the-issue-the-relative-virtual-path-site.mobile.master-is-not-allowed-here-in-asp.net-friendly-urls.html




回答7:


I solved it simply adding at the end of "Page_Load" event of "ViewSwitcher.ascx" the redirection saved by default: Response.Redirect(url) So the sub results in:

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



回答8:


I found it easier to just delete (or rename) Site.Mobile.Master and ViewSwitcher.ascx. This seemed to work just fine for me.



来源:https://stackoverflow.com/questions/20677905/asp-net-turn-off-friendlyurls-mobile-master-page

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!