C# MVC 4移动和平板电脑视图分离

C# MVC 4移动和平板电脑视图分离,c#,asp.net-mvc,C#,Asp.net Mvc,我正在尝试为平板电脑和手机创建单独的视图。在app_start中,我有此代码 DisplayModeProvider.Instance.Modes.Insert(0, new DefaultDisplayMode("Tablet") { ContextCondition = (ctx => ctx.Request.UserAgent.IndexOf("iPad", StringComparison.Ordina

我正在尝试为平板电脑和手机创建单独的视图。在app_start中,我有此代码

DisplayModeProvider.Instance.Modes.Insert(0, new
        DefaultDisplayMode("Tablet")
        {
            ContextCondition = (ctx =>
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0)
        });
我已经创建了两个布局文件,一个用于移动设备,一个用于平板电脑。但当我从安卓上的移动设备访问时,会有冲突。它将我重定向到layout.tablet。我怎样才能把这两台设备分开呢?

neowinian

尝试添加一个额外的逻辑片段:

&& ctx.Request.UserAgent.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) < 0

你有没有看过这样的服务,所有的用户代理/设备都为你做了繁重的工作?虽然它不是免费的,但他们提供了一个“lite”版本,为您提供了很多您需要的东西,尽管我注意到“iTablet”在他们的高级版本中有所体现。

我已经用浏览器中的user agent switcher对此进行了测试,效果很好

DisplayModeProvider.Instance.Modes.Insert(0, new
        DefaultDisplayMode("Tablet")
        {
            ContextCondition = (ctx =>
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0 && 
            ctx.Request.UserAgent.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) < 1)
        });

        DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Mobile")
        {
            ContextCondition = (ctx =>
                ctx.GetOverriddenBrowser().IsMobileDevice)
        });
DisplayModeProvider.Instance.Modes.Insert(0,新)
默认显示模式(“平板电脑”)
{
ContextCondition=(ctx=>
ctx.Request.UserAgent.IndexOf(“iPad”,StringComparison.OrdinalIgnoreCase)>=0||
ctx.Request.UserAgent.IndexOf(“Android”,StringComparison.OrdinalIgnoreCase)>=0&&
ctx.Request.UserAgent.IndexOf(“mobile”,StringComparison.OrdinalIgnoreCase)<1)
});
DisplayModeProvider.Instance.Modes.Insert(1,新的DefaultDisplayMode(“移动”)
{
ContextCondition=(ctx=>
ctx.getOverridedBrowser().IsMobileDevice)
});

您可以使用51Degrees的免费云解决方案来帮助您检测不同的设备类型。使用IsMobile和IsTablet属性,可以根据结果重定向。您可以从网站获得免费云产品,并获得免费云密钥。有关如何使用API的信息,请参见此处的教程

例如,您可以请求返回如下所示的设备类型,然后根据响应输入重定向逻辑

@using (var webClient = new System.Net.WebClient())
{
  string json = webClient.DownloadString(String.Format(
  "https://cloud.51degrees.com/api/v1/{0}/match?user-agent=
{1}&values=DeviceType",
  "YOUR KEY HERE",
  HttpUtility.UrlEncode(Request.UserAgent)));

dynamic match = Newtonsoft.Json.Linq.JObject.Parse(json);
  SortedList<string, string[]> values = Newtonsoft.Json.JsonConvert.DeserializeObject<SortedList<string, string[]>>(match.Values.ToString());
  string[] hvValues;

 if (values.TryGetValue("DeviceType", out hvValues))
  {
foreach (string s in hvValues)
{
<h4>
    Device Type:
    @s
</h4>
}
  }
使用(var webClient=new System.Net.webClient()) { string json=webClient.DownloadString(string.Format( "https://cloud.51degrees.com/api/v1/{0}/匹配?用户代理= {1} &values=DeviceType“, “这里是你的钥匙”, HttpUtility.UrlEncode(Request.UserAgent)); 动态匹配=Newtonsoft.Json.Linq.JObject.Parse(Json); SortedList values=Newtonsoft.Json.JsonConvert.DeserializeObject(match.values.ToString()); 字符串[]值; if(values.TryGetValue(“DeviceType”,out hvValues)) { foreach(hvValues中的字符串s) { 设备类型: @ } }
披露:我在51度工作。

我试过了,但MVC4的移动功能对我来说相当不错。嘿-呸,你把我的荣耀夺回来了!!:)
DisplayModeProvider.Instance.Modes.Insert(0, new
        DefaultDisplayMode("Tablet")
        {
            ContextCondition = (ctx =>
            ctx.Request.UserAgent.IndexOf("iPad", StringComparison.OrdinalIgnoreCase) >= 0 ||
            ctx.Request.UserAgent.IndexOf("Android", StringComparison.OrdinalIgnoreCase) >= 0 && 
            ctx.Request.UserAgent.IndexOf("mobile", StringComparison.OrdinalIgnoreCase) < 1)
        });

        DisplayModeProvider.Instance.Modes.Insert(1, new DefaultDisplayMode("Mobile")
        {
            ContextCondition = (ctx =>
                ctx.GetOverriddenBrowser().IsMobileDevice)
        });
@using (var webClient = new System.Net.WebClient())
{
  string json = webClient.DownloadString(String.Format(
  "https://cloud.51degrees.com/api/v1/{0}/match?user-agent=
{1}&values=DeviceType",
  "YOUR KEY HERE",
  HttpUtility.UrlEncode(Request.UserAgent)));

dynamic match = Newtonsoft.Json.Linq.JObject.Parse(json);
  SortedList<string, string[]> values = Newtonsoft.Json.JsonConvert.DeserializeObject<SortedList<string, string[]>>(match.Values.ToString());
  string[] hvValues;

 if (values.TryGetValue("DeviceType", out hvValues))
  {
foreach (string s in hvValues)
{
<h4>
    Device Type:
    @s
</h4>
}
  }