c# - 使用 C# 读取 userAgent

标签 c# asp.net-mvc-3

我有以下代码读取 userAgent 并根据使用 indexOf 匹配的值执行一些逻辑:

String userAgent;
userAgent = Request.UserAgent;
// If it's not IE
if (userAgent.IndexOf("MSIE") < 0)
{
    return RedirectToAction("Index", "Home", new { area = "Dashboard" });
}
// If it's IE BUT ChromeFrame
else if(userAgent.IndexOf("ChromeFrame") > -1)
{
    return RedirectToAction("Index", "Home", new { area = "Dashboard" });
}
// It's just IE
else
{
    return View("ChromeFrame");
}

如果它是 IE,那么它应该返回 View ,或者如果它的 IE 但包含 ChromeFrame,那么它应该重定向,如果它是另一个浏览器,那么它也应该重定向。

我认为问题出在代码的 > 0 部分。比较信息的正确方法是什么?谢谢。

最佳答案

只需使用 contains method ,这将使您的代码不那么晦涩难懂且不易出错。

if (userAgent.Contains("MSIE"))
{
    return RedirectToAction("Index", "Home", new { area = "Dashboard" });
}

关于c# - 使用 C# 读取 userAgent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9031777/

相关文章:

c# - 使用 LINQ 旋转数据

c# - 如何修改旋转变换的位置

c# - 如何在 WCF 中识别经过身份验证的用户?

asp.net - ASP.NET MVC 之上的一些框架可以轻松创建网站的后端部分?

c# - 如何通过 WebRequest 调用 MVC 操作并通过 Active Directory 验证请求?

c# - MSBuild 后期构建

c# - 添加程序集引用后,类型或命名空间名称 'ComponentModel' 在命名空间中不存在

html - HTML 呈现的 MVC3 TextArea 问题

css - 如何将类添加到 Razor 形式并提供填充?

javascript - 如何在 asp.net mvc 3 中完全禁用 javascript 验证