Blunderfest
Blunderfest

Reputation: 1854

How to access browser version in class

I'm writing a vb.net svg workflow viewer, but need to do browser detection to see if I should revert to VML or if SVG is supported. Our targeted browser is IE, and I know that to detect browser version in VB you can do the following:

Dim myBrowserCaps As System.Web.HttpBrowserCapabilities = Request.Browser
If (CType(myBrowserCaps, System.Web.Configuration.HttpCapabilitiesBase)).Browser.ToUpper().IndexOf("IE") >= 0 Then
    labelText = "Browser is Internet Explorer."
Else
    labelText = "Browser is not Internet Explorer."
End If

Or something similiar.

The problem is, that while I can detect the browser and UserAgent string directly from my .aspx page, when I put the same code into a class that is referenced by a webpart on the page (yes webpart, it's a legacy product) I get an error. The webpart is referenced by many pages, which is why I want the browser detection in the class instead of on the page.

Dim Browser As HttpBrowserCapabilities = Request.Browser
If Browser.Browser = "IE" Then
    'IE Junk
End If

The error is simply "'Request' is not declared. It may be inaccessible due to its protection level".

This code is copied directly from the MSDN documentation and for the life of me I can't find anywhere that explains how or if I can do this from a class. I've browser Stack, I've also browsed their documentation to no avail.

I've already tried HttpRequest.Browser instead, but this got me no where.

So, my two questions are...

Edit

Originally I had tried using the current context, but I had done it incorrectly. I had tried:

Dim browser as HttpBrowserCapabilities = HttpContext.Request.Browser

When it should have been:

Dim browser as HttpBrowserCapabilities = HttpContext.Current.Request.Browser

So I was wrong syntactically (and in understanding)

I think the question would probably still be helpful, as I couldn't find it reproduced anywhere else.

Thanks to all!

Upvotes: 2

Views: 849

Answers (1)

Blunderfest
Blunderfest

Reputation: 1854

Per SLaks comment, the HttpContext must be passed in to access the browser from the request. I ended up using.

 Dim context As HttpContext = HttpContext.Current
 Dim Browser As HttpBrowserCapabilities = context.Request.Browser

Upvotes: 2

Related Questions