After installing Microsoft.AspNet.FriendlyUrls to hide .aspx from URL, The website is showing 500 internal server error only on mobile devices

After installing Microsoft.AspNet.FriendlyUrls to hide .aspx from URL, The website is showing 500 internal server error only on mobile devices, but it works fine on the desktop.

Code in RouteConfig.cs

using System;
using System.Collections.Generic;
using System.Web;
using System.Web.Routing;
using Microsoft.AspNet.FriendlyUrls;

namespace ASP
{
    public static class RouteConfig
    {
        public static void RegisterRoutes(RouteCollection routes)
        {
            var settings = new FriendlyUrlSettings();
            settings.AutoRedirectMode = RedirectMode.Permanent;
            routes.EnableFriendlyUrls(settings);
        }
    }
}

Code in Global.asax

<%@ Application Language="C#" %>

<script runat="server">

    void Application_Start(object sender, EventArgs e) 
    {
        RouteConfig.RegisterRoutes(System.Web.Routing.RouteTable.Routes);
        // Code that runs on application startup

    }
    
    void Application_End(object sender, EventArgs e) 
    {
        //  Code that runs on application shutdown

    }
        
    void Application_Error(object sender, EventArgs e) 
    { 
        // Code that runs when an unhandled error occurs

    }

    void Session_Start(object sender, EventArgs e) 
    {
        // Code that runs when a new session is started

    }

    void Session_End(object sender, EventArgs e) 
    {
        // Code that runs when a session ends. 
        // Note: The Session_End event is raised only when the session-state mode
        // is set to InProc on the Web. config file. If session mode is set to StateServer 
        // or SQLServer, the event is not raised.

    }

I could Log the error when the website opens on Mobile devices.

This is the error in the log file:

System.Web.HttpUnhandledException (0x80004005): Exception of type 'System.Web.HttpUnhandledException' was thrown. ---> System.ArgumentException: The relative virtual path 'masterpage/MasterPage.Mobile.Master' is not allowed here.
   at System.Web.VirtualPath.Create(String virtualPath, VirtualPathOptions options)
   at Microsoft.AspNet.FriendlyUrls.Abstractions.VirtualPathUtilityWrapper.ToAppRelative(String virtualPath)

  • Errors like this are usually due to different default HTTP headers being used by different browsers (or api libraries). This may be the USER-AGENT header. The solution is to add the same USER-AGENT header the works in desktop to the mobile applications. I would use Postman to make the request. Then open the Postman console to get the complete HTTP Request that is used on the desktop. Then add any missing HTTP header to the mobile code. The mobile apps are completing the HTTP connection but then server is getting an exception while processing the data.

    – 

  • I would delete the SiteMobile.Master page if you have one. You not likely using the mobile master page, so delete it

    – 

  • @jdweng I’m not using a separate mobile code. The code is the same for both desktop and mobile versions. I’m only using CSS media queries to style the website for tab and mobile devices.

    – 

  • I’ve tried deleting SiteMobile.Master and ViewSwitcher.ascx but it does not change the problem on mobile. @AlbertD.Kallal

    – 

  • You are interfacing with the OS and the connection is being made in OS methods. Try doing what I said. It should work.

    – 

Leave a Comment