2

I know in ASP.NET MVC you can have mobile views and do things like Index.mobile.cshtml and _Layout.mobile.cshtml and the server knows to serve these views/pages to mobile devices rather than Index.cshtml and _Layout.cshtml, but can this also be done in plain ASP.NET websites (not using MVC)?

Note : I am using razor syntax in the plain ASP.NET website.

Thanks in advance.

-- Lee

UPDATE :

To clarify, I am aware of the various browser detection methods. My question is specifically about whether mobile views in the form Index.mobile.cshtml are available in plain ASP.NET.

UPDATE (Functionality now included in ASP.NET latest release announced 18th February 2013) :

Talk of the devil.. this is now possible in a recent release.. Scroll down the page to the heading 'ASP.NET Web Forms Enhancements'

http://weblogs.asp.net/scottgu/archive/2013/02/18/announcing-release-of-asp-net-and-web-tools-2012-2-update.aspx

Lee Englestone
  • 4,545
  • 13
  • 51
  • 85
  • I'm not aware of such feature in ASP.Net WebForms, AFAIK, you're going to have to detect a mobile device (http://stackoverflow.com/questions/2022897/how-to-detect-a-mobile-phone-in-a-web-application). However, you may want to consider more on the topic, such as, SEO impacts, redirecting to a mobile site using URL (m.yoursite.com). Also, you may want to read on responsive design. – Allov Jan 30 '13 at 15:49
  • Update : This may be possible in latest release http://weblogs.asp.net/scottgu/archive/2013/02/18/announcing-release-of-asp-net-and-web-tools-2012-2-update.aspx – Lee Englestone Feb 25 '13 at 10:02

3 Answers3

3

Take a look at this, brief description of mobile support for ASP.NET Web Forms:

How To: Add Mobile Pages to Your ASP.NET Web Forms / MVC Application

Jamie Keeling
  • 9,806
  • 17
  • 65
  • 102
  • Thanks, there are some pointers on there but it doesn't answer my question about the possibility of having Index.mobile.cshtml – Lee Englestone Jan 30 '13 at 15:50
0

My understanding is that the alternate view modes support native to MVC4 is a result of the WebPages2 used by MVC, and is currently only used within MVC to resolve locating Views via the View Engine (combination of VirtualPathProvider and DisplayModeProvider). This is because views served up via MVC and requests via the URI do not map to physical locations on the server to serve up files from. ASP.NET on the other hand serves up files directly based off of the URI, and does not depend on a virtual path provider the way MVC does.

My guess would be that ASP.NET does not support automatically serving up alternate files based off of the same framework that MVC uses. That being said, I'm sure it would be possible to derive an implementation based off of the of the logic of VirtualPathProviderViewEngine that could achieve a similar result with ASP.NET, however I know of no implementation that does this currently. Best suggestion would be to see if you can find usages of DisplayModeProvider and see if anything pops up.

I'm not an expert so feel free keep looking but I thought I would offer what I can.

Nick Albrecht
  • 16,607
  • 10
  • 66
  • 101
  • I thought as much. Do you think it might me as simple interrogating each `Application_BeginRequest() in Global.asax`, checking `Request.Browser.IsMobileDevice`, then checking if the requested file has an equivalent `.mobile.cshtml` file and if so.. redirecting there? – Lee Englestone Jan 31 '13 at 08:37
  • Worth a shot, `Request.Browser.IsMobileDevice` depends on the Browser Definition file which in the past hasn't been known to be up to date, but I just took a look online and it seems they did update it with .NET 4.0. I also just dug though `DisplayModeProvider` and found out they use `HttpContext.GetOverriddenBrowser().IsMobileDevice` so give that a shot. – Nick Albrecht Jan 31 '13 at 18:30
0

So I have come to the conclusion that this is not built in functionality in plain ASP.NET. Though there are (IMO less elegant) alternatives.

Are ASP.NET mobile views only for ASP.NET MVC? Yes.

Update : This may be possible in latest release

http://weblogs.asp.net/scottgu/archive/2013/02/18/announcing-release-of-asp-net-and-web-tools-2012-2-update.aspx

-- Lee

Lee Englestone
  • 4,545
  • 13
  • 51
  • 85