ASP.NET MVC 4移动功能 [英] ASP.NET MVC 4 Mobile Features
问题描述
我想出来的新ASP.NET MVC 4移动功能。我做了一个简单的应用程序只用一个控制器(HomeController的)和一个视图(指数)。我还添加索引视图的移动版本。
查看/主页/ Index.cshtml
查看/主页/ Index.Mobile.cshtml
当启动在桌面浏览器应用程序显示在常规视图不如预期,但是当我启动在应用Opera移动的模拟器作为三星Galaxy S,我仍然得到常规视图,而不是移动版本。
从仿真器发送的用户代理字符串看起来是这样的:
歌剧院/ 9.80(Windows NT的6.1;歌剧牧高笛/ 23731; U; EN)preSTO / 2.9.201版本/ 11.50
为什么任何想法是不工作?
更新
由于@nemesv我能够解决的问题,这是我目前的解决办法,希望它会覆盖大部分移动场景。
公共类MobileDisplayMode:DefaultDisplayMode
{
私人只读StringCollection _useragenStringPartialIdentifiers =新StringCollection
{
Android的,
移动,
歌剧牧高笛
三星,
HTC,
诺基亚,
爱立信
索尼爱立信,
苹果手机
}; 公共MobileDisplayMode():基地(手机)
{
ContextCondition =(上下文=> IsMobile(context.GetOverriddenUserAgent()));
} 私人布尔IsMobile(字符串useragentString)
{
返回_useragenStringPartialIdentifiers.Cast<串GT;()
。任何(VAL => useragentString.IndexOf(VAL,StringComparison.InvariantCultureIgnoreCase)> = 0);
}
}
和我的Global.asax
DisplayModeProvider.Instance.Modes.Insert(0,新MobileDisplayMode());
ASP.Net(实际上是 HttpBrowserCapabilitiesBase
类)不承认的Opera Mobile模拟器作为移动浏览器。
您可以在任何控制器动作检查: HttpContext.Request.Browser.IsMobileDevice
将返回假
的Opera移动浏览器。
由于内置的 DefaultDisplayMode
使用下面的方法来检查您需要注册您的自定义移动浏览器 DISPLAYMODE
这正确识别的Opera Mobile。
要做到这一点,你需要它添加到Global.asax中的Application_Start
:
DisplayModeProvider.Instance.Modes.Insert(0,新DefaultDisplayMode(手机)
{
ContextCondition =(上下文=> context.GetOverriddenUserAgent()
.IndexOf(歌剧牧高笛,StringComparison.OrdinalIgnoreCase)GT; = 0)
});
I'm trying out the new ASP.NET MVC 4 Mobile Features. I made a simple app with just one controller (HomeController) and one view (Index). I also added a mobile version of the index view.
Views/Home/Index.cshtml
Views/Home/Index.Mobile.cshtml
When launching the application in a desktop browser the regular view is shown as expected, however when I launch the application in the Opera Mobile Emulator as a Samsung Galaxy S, I still get the regular view and not the mobile version.
the user agent string sent from the emulator looks like this:
Opera/9.80 (Windows NT 6.1; Opera Mobi/23731; U; en) Presto/2.9.201 Version/11.50
Any ideas on why this is not working?
Update Thanks to @nemesv I was able to solve the problem, here is my current solution, hopefully it will cover most mobile scenarios.
public class MobileDisplayMode : DefaultDisplayMode
{
private readonly StringCollection _useragenStringPartialIdentifiers = new StringCollection
{
"Android",
"Mobile",
"Opera Mobi",
"Samsung",
"HTC",
"Nokia",
"Ericsson",
"SonyEricsson",
"iPhone"
};
public MobileDisplayMode() : base("Mobile")
{
ContextCondition = (context => IsMobile(context.GetOverriddenUserAgent()));
}
private bool IsMobile(string useragentString)
{
return _useragenStringPartialIdentifiers.Cast<string>()
.Any(val => useragentString.IndexOf(val, StringComparison.InvariantCultureIgnoreCase) >= 0);
}
}
And i Global.asax
DisplayModeProvider.Instance.Modes.Insert(0, new MobileDisplayMode());
ASP.Net (actually the HttpBrowserCapabilitiesBase
class) doesn't recognize the Opera Mobile Emulator as a Mobile browser.
You can check this in any controller action: HttpContext.Request.Browser.IsMobileDevice
will return false
for the Opera Mobile browser.
Because the built in DefaultDisplayMode
uses the following method to check mobile browsers you need to register your custom DisplayMode
which correctly recognizes Opera Mobile.
To do this you need to add this to the Global.asax Application_Start
:
DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Mobile")
{
ContextCondition = (context => context.GetOverriddenUserAgent()
.IndexOf("Opera Mobi", StringComparison.OrdinalIgnoreCase) >= 0)
});
这篇关于ASP.NET MVC 4移动功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!