Search code examples
asp.nettwitter-bootstrapvisual-studio-2013friendly-url

asp.NET ViewSwitcher


There seems to be very little information available about Friendly URL's and ViewSwitcher.

After creating a file Default.Mobile.aspx and assigning it a master page Site.Mobile.Master then running it a 404 'not found' error is always returned.

Any ideas what could be going wrong, and/or is there any good reference material for this? The only stuff I can find is from 2012 or older.

IIS is v7.0 (but VS 2013 is emulating IIS 8.0). Project is web forms with MVC included. Bootstrap is being used

Thank you

=========================== EDIT ==========================================

I have now tried creating three separate projects 'out of the box'

Web | ASP.Net web application Forms and adding MVC and/or API or just plain forms

Add the Default.Mobile.aspx file and assign it to Site.Mobile.Master - each time I get the same 404 error when it's run

I then created a Web Forms project - Web | Visual Studio 2012 | ASP.NET Web Forms Application. With this the Default.Mobile.aspx form will run and will switch to the main default file, but will not switch back again.

On all flavours the View Switcher control just displays as 'view | Switch to' and appears not to be loading ViewSwitcher.ascx and showing 'x view | Switch to y view'

===================== EDIT 01 OCT 2014 =========================

Solved the problem of ViewSwitcher.ascx. The Page_Load event in ViewSwithcher.ascx.vb wasn't wired up - added 'Handles MyBase.Load' to the end and that now displays the correct text, but still can't get it to show the mobile view - sigh


Solution

  • This is the complete Page_Load code that enables switching between Desktop and Mobile pages. Its includes Handles MyBase.Load and .__FriendlyUrls_SwitchViews = True extra parameter

         Protected Sub Page_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            ' Determine current view
            Dim isMobile = WebFormsFriendlyUrlResolver.IsMobileView(New HttpContextWrapper(Context))
            CurrentView = If(isMobile, "Mobile", "Desktop")
    
            ' Determine alternate view
            AlternateView = If(isMobile, "Desktop", "Mobile")
    
            Dim strSwitchViewRouteName As String = "AspNet.FriendlyUrls.SwitchView"
            Dim SwitchViewRoute = RouteTable.Routes(strSwitchViewRouteName)
            If SwitchViewRoute Is Nothing Then
                ' Friendly URLs is not enabled or the name of the switch view route is out of sync
                Me.Visible = False
                Return
            End If
            ' Create switch URL from the route, e.g. ~/__FriendlyUrls_SwitchView/Mobile?ReturnUrl=/Page
            Dim url = GetRouteUrl(strSwitchViewRouteName, New With { _
                Key .view = AlternateView, .__FriendlyUrls_SwitchViews = True _
            })
            url += "?ReturnUrl=" & HttpUtility.UrlEncode(Request.RawUrl)
            SwitchUrl = url
        End Sub