用C#读取userAgent
发布时间:2020-12-15 08:27:52 所属栏目:百科 来源:网络整理
导读:我有以下代码读取userAgent并根据使用indexOf匹配的值执行一些逻辑: String userAgent;userAgent = Request.UserAgent;// If it's not IEif (userAgent.IndexOf("MSIE") 0){ return RedirectToAction("Index","Home",new { area = "Dashboard" });}// If it
我有以下代码读取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",new { area = "Dashboard" }); } // It's just IE else { return View("ChromeFrame"); } 如果它是IE,那么它应该返回视图,或者如果它的IE但包含ChromeFrame,那么它应该重定向,它是另一个浏览器,那么它也应该重定向. 我认为问题在于> 0部分代码.比较信息的正确方法是什么?谢谢. 解决方法
只需使用
contains method,这将使您的代码不那么神秘,不易出错.
if (userAgent.Contains("MSIE")) { return RedirectToAction("Index",new { area = "Dashboard" }); } (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |