使用C#读取userAgent [英] Reading the userAgent with C#

查看:459
本文介绍了使用C#读取userAgent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code which reads the userAgent and does some logic based on the values matched using 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,则它应该返回视图,或者如果它的IE但包含ChromeFrame,则它应该重定向,并且它是另一个浏览器,那么它也应该重定向.

If it's IE then it should return the view or if its IE but contains ChromeFrame then it should redirect and it's another browser then it should redirect as well.

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

I think the problem is with the > 0 part of the code. What is the correct way of comparing info? Thanks.

推荐答案

只需使用

Just use the contains method, which will make your code less cryptic and less error-prone.

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

这篇关于使用C#读取userAgent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆