Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET turn off FriendlyURLs mobile.master page

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

like image 335
Piotr Stulinski Avatar asked Dec 19 '13 09:12

Piotr Stulinski


3 Answers

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

like image 163
Levi Avatar answered Nov 11 '22 08:11

Levi


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());
    }
like image 22
MikeBaz - MSFT Avatar answered Nov 11 '22 08:11

MikeBaz - MSFT


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);
like image 29
tslin Avatar answered Nov 11 '22 06:11

tslin