Verifying browser and version with REGEX

1

I'm working on a vb.net MVC system and need to detect the browser and version.

I'll only use Chrome, and IE9 +. I would like to know if I can simplify the verification structure using Regex.

Below is the excerpt that I use to do the verification.

If (Not Request.Browser.ToString().ToLower().Contains("ie") And
        Not Request.Browser.ToString().ToLower().Contains("internet explorer") And
        Not Request.Browser.ToString().ToLower().Contains("chrome")) Then

        Return View("IncompativelAgent")
    Else
        If Not Request.Browser.ToString().ToLower().Contains("chrome") And Request.Browser.MajorVersion < 9 Then
            Return View("IncompativelAgent")
        End If
    End If

I find it very inelegant in the way it is.

    
asked by anonymous 06.01.2016 / 16:50

1 answer

3

Following this link for getting the information:

C#

System.Web.HttpBrowserCapabilities browser = Request.Browser;

string pattern = @"ie|internet explorer|chrome";

MatchCollection matches = Regex.Matches(browser.Browser, pattern, RegexOptions.IgnoreCase)

if(matchs.Count == 0 And browser.Version < 9){
    Return View("IncompativelAgent");
}

VB

    Dim regex As Regex = New Regex("ie|internet explorer|chrome", RegexOptions.IgnoreCase)
    Dim match As Match = regex.Match(Request.Browser.Browser)
    If (Not match.Success Or (match.Success And Request.Browser.MajorVersion < 9)) Then
        Return View("IncompativelAgent")
    End If

Obs

I'm not a connoisseur, I'm based on research.

Links

How to: Detect Browser Types and Browser Capabilities in ASP.NET Web Forms Check Browser version and name (VB.net) VB.NET Regex.Match Function Examples

    
06.01.2016 / 17:47