zoukankan      html  css  js  c++  java
  • ASP.NET 根据 UserAgent 获取浏览器的类型和版本

    //using System.Text.RegularExpressions;
    public string GetBrowserName(string userAgent, out string browserName, out string ver)
    {
        string fullBrowserName = string.Empty;
        browserName = string.Empty;
        ver = string.Empty;
        // IE
        string regexStr = @"msie (?<ver>[\d.]+)";
        Regex r = new Regex(regexStr, RegexOptions.IgnoreCase);
        Match m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Firefox
        regexStr = @"firefox\/([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Chrome
        regexStr = @"chrome\/([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Opera
        regexStr = @"opera.([\d.]+)";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        // Safari
        regexStr = @"version\/([\d.]+).*safari";
        r = new Regex(regexStr, RegexOptions.IgnoreCase);
        m = r.Match(userAgent);
        if (m.Success)
        {
            browserName = "IE";
            ver = m.Groups["ver"].Value;
            fullBrowserName = string.Format("{0} {1}", browserName, ver);
            return fullBrowserName;
        }
        return fullBrowserName;
    }
    
  • 相关阅读:
    fetch的使用--当无法判断后台返回数据为什么类型时如何操作
    单页面与多页面间的区别及优缺点
    关于倒计时在关屏后不准确的问题
    前端分页仿百度分页效果
    pc端的弹性布局适配方案
    前端性能优化方向
    居民身份证号码组成规则
    axios简单介绍
    es6 promise 简单总结
    js原型链和原型链的继承
  • 原文地址:https://www.cnblogs.com/anjou/p/3114084.html
Copyright © 2011-2022 走看看