Fixes For the issue "The relative virtual path 'Site.Mobile.Master' is not allowed here" of FriendlyUrls in ASP.NET

Why this Site.Mobile.Master issue appear ?


If you are using the FriendlyURLs of ASP.NET in order to remove the extensions from your web pages then you might have faced this problem while browsing you site in mobile devices.

Its actually a bug in the current version of Web Forms friendly URLs (1.0.2) which tries to find the site.mobile.master whenever you try to open up your website in a mobile device and if you have not that master page in your solution then it will end up by giving you this shit error "The relative virtual path 'Site.Mobile.Master' is not allowed here".

How To Fix this Site.Mobile.Master Issue ?


To fix this issue you need to override the method TrySetMobileMasterPage() of  Microsoft.AspNet.FriendlyUrls.

TrySetMobileMasterPage() this method is the culprit here for the issue which was searching for the site.mobile.master page in the solutions and if you do not have it then it ends up giving you the error.



Hence our goal is to override this method. Now create a class named SiteMobileMasterFriendlyUrlResolver  and inside it we will override the method TrySetMobileMasterPage().

Here is the code for the newly created resolver class.
/// <summary>
/// Override the method TrySetMobileMasterPage by returning false while it tries to find the site.mobile.master page in the solution
/// </summary>
public class SiteMobileMasterFriendlyUrlResolver : Microsoft.AspNet.FriendlyUrls.Resolvers.WebFormsFriendlyUrlResolver
{
    protected override bool TrySetMobileMasterPage(HttpContextBase httpContext, Page page, string mobileSuffix)
    {
        return false;
        //return base.TrySetMobileMasterPage(httpContext, page, mobileSuffix);
    }
}
Now in the RouteConfig class change the routes.EnableFriendlyUrls(settings) method, like below.
public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings, new SiteMobileMasterFriendlyUrlResolver());
        }
    }
}


Now you can run your application in any mobile device and you will not see this error again. as we have overridden the code.

Happy Coding...

8 comments: