How does MVC4 detect a mobile browser?

做~自己de王妃 提交于 2019-12-09 15:54:03

问题


I've just followed Scott Hanselmans tutorial on making a switchable site in MVC4.

I'm using the Opera mobile emulator to test as I don't have VS2102.

When rendering my page in the emulator Request.Browser.IsMobileDevice is false although it still renders the mobile view.

My questions are:

  • Why/How does MVC4 decide to render the mobile view? It doesn't seem like it looks at Request.Browser.IsMobileDevice.
  • Will the View Switcher be displayed on a real mobile device?

Edit:

I think I realised my mistake:

To get it to work with the opera emulator I added a MobileDisplayMode as described here: ASP.NET MVC 4 Mobile Features

It seems that that solution works with rendering correctly but Request.Browser.IsMobileDevice won't work. The same goes for ViewContext.HttpContext.GetOverriddenBrowser().IsMobileDevice is there a way to get around this?


回答1:


MVC recognize Mobile device by matching the HTTP request header with browser config files present in C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\Browsers

The asp.net MVC browser detection approach is not great so i recommend using some sort of DDR



来源:https://stackoverflow.com/questions/12710026/how-does-mvc4-detect-a-mobile-browser

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!