我有以下代码读取 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/