23

My Website is responsive with Twitter Bootstrap and the desktop pages are designed for tablets and desktop. aspnet.friendlyUrls considers tablet devices as mobile and sends them to the ".Mobile.aspx" equivalent. How can I disable this behavior and keep tablets on the desktop pages?

2 Weeks later and still no awnser or even a comment? Am i the only one who actually uses aspnet.FriendlyUrls even if it's distributed in new VS2013 Asp.Net projects by default?

Jeroen
  • 343
  • 1
  • 2
  • 12

2 Answers2

44

There is no setting to turn this off, but you can disable this by deleting the Site.Mobile.Master and ViewSwitcher files

The following files are no longer needed:

Site.Mobile.Master
- Site.Mobile.Master.cs
- Site.Mobile.Master.designer.cs

ViewSwitcher
- ViewSwitcher.cs
- ViewSwitcher.ascx.cs

Thanks to @fortboise for simplifying my answer

Thom
  • 577
  • 6
  • 13
  • 3
    Shouldn't the line <%@ Register Src="~/ViewSwitcher.ascx" TagPrefix="friendlyUrls" TagName="ViewSwitcher" %> be found in Site.Mobile.Master instead of Site.Master.cs? – Chun Lin Jun 14 '15 at 14:39
  • 2
    @ChunLin that's where I found it as well. I went in and updated the answer accordingly. – James Skemp Oct 12 '15 at 12:00
  • 2
    This solution did not work in my my case (using visual studio community 2015) when commenting and replacing mentioned lines the solution keeps loading Site.Mobile.Master for mobile devices...any suggestions or forgotten steps? – Pedro Emilio Borrego Rached Oct 10 '16 at 13:51
  • Likewise for me, the mod to RouteConfig.cs does not get it done. What it looks like that does is to remove the friendlyUrls-generated link to "switch to desktop" in the Mobile.Master. (The sln won't publish unless one also removes the tag in the body of the ascx) – fortboise Feb 15 '17 at 16:42
  • 1
    @PedroEmilioBorregoRached can you try deleting the Site.Mobile.Master and ViewSwitcher files also? You dont need those files because you dont want a diffrent master page for mobile devices anyway. – Thom Feb 15 '17 at 18:27
  • @fortboise check the answer I gave above. – Thom Feb 15 '17 at 18:28
  • 1
    Followed your answer from top down, initial results as commented, problem solved by what you described as "optionally." Testing stepwise, I see that what accomplishes "just use one template, do not switch" is to **DELETE THE Site.Mobile.Master** (and its .cs and designer files). That--JUST that--seems to be the solution. ViewSwitcher can go too, as it's orphaned (unless you provided for a switch BACK in the Site.Master). (The original question seems misframed: FriendlyUrls are not what switch templates for variant user agents.) – fortboise Feb 16 '17 at 21:57
  • @fortboise so if I understood you correctly, the first and the next step aren't necessary? This would make things much simpler. – Thom Feb 17 '17 at 08:49
  • 1
    That was my experience. Starting with "everything" out of the box, and deleting the Site.Mobile.Master stack stops switching. Didn't hurt to simplify RegisterRoutes and chuck the ViewSwitcher ascx as well, but that wasn't essential. – fortboise Feb 18 '17 at 14:32
  • Deleting the Site.Mobile.Master file and the ViewSwitcher files is step 1. Step 2 is checking, 'remove additional files at destination' in the File Publish options in the Publishing Settings on Visual Studio. – Olorunfemi Davis Mar 26 '19 at 11:11
8

Remove the won't solve the problem, the way is override the TrySetMobileMasterPage.

step one: Create a Class

public class MyWebFormsFriendlyUrlResolver : Microsoft.AspNet.FriendlyUrls.Resolvers.WebFormsFriendlyUrlResolver
{
protected override bool TrySetMobileMasterPage(HttpContextBase httpContext, Page page, String mobileSuffix)
{
    if (mobileSuffix == "Mobile")
    {
        return false;
    }
    else
    {
        return base.TrySetMobileMasterPage(httpContext, page, mobileSuffix);
    }
}

}

After go in App_Start/RouteConfig and set:

public static void RegisterRoutes(RouteCollection routes)
    {
        var settings = new FriendlyUrlSettings();
        settings.AutoRedirectMode = RedirectMode.Permanent;
        routes.EnableFriendlyUrls(settings, new MyWebFormsFriendlyUrlResolver());
    } 
  • Deleting the Site.Mobile.Master file and the ViewSwitcher files is step 1. Step 2 is checking, 'remove additional files at destination' in the File Publish options in the Publishing Settings on Visual Studio. – Olorunfemi Davis Mar 26 '19 at 11:11
  • Just a note, this code allows me to have the master page be determined at the page level in the markup and not by the routing code based on device. thank you this is great. – Taylor Brown Jul 16 '19 at 21:34
  • thank you for this. i have been banging my head to this. i can understand its use but if you know css this feature is kinda pointless. i do like the friendly URL makes the url a lot nicer. – Cees Jun 02 '21 at 08:51