HOW TO:在 ASP.NET Web 網頁中偵測瀏覽器類型
更新:2007 年 11 月
不同的瀏覽器和不同版本的相同瀏覽器,可支援不同的功能。在您的應用程式中,您可能需要判斷使用者是使用哪種類型的瀏覽器檢視網頁,或是判斷瀏覽器是否支援某些功能。
注意事項: |
---|
ASP.NET 可以自動判斷瀏覽器能力,並使用此資訊來呈現 ASP.NET 伺服器控制項的適當 HTML 標記。如需詳細資訊,請參閱 ASP.NET 伺服器控制項和瀏覽器能力。 |
若要在 ASP.NET Web 網頁中偵測瀏覽器類型
查詢 Browser 屬性,其中包含 HttpBrowserCapabilities 物件。這個物件在 HTTP 要求中從瀏覽器或用戶端裝置取得資訊,告訴應用程式瀏覽器或用戶端裝置所提供支援的類型和層級。這個物件接著會使用強型別 (Strongly Typed) 屬性和泛型名稱/值字典,公開 (Expose) 關於瀏覽器能力的資訊。
在下列程式碼範例中,示範了如何在網頁的文字方塊中顯示瀏覽器資訊。
注意事項: HttpBrowserCapabilities 物件所公開的屬性會指出瀏覽器的固有能力,但是不一定會反映目前的瀏覽器設定。例如,Cookies 屬性會指出瀏覽器是否原本就支援 Cookie,但不會指出提出要求的瀏覽器是否已啟用 Cookie。如需詳細資訊,請參閱 ASP.NET Cookie 概觀。
Private Sub Button1_Click(ByVal sender As System.Object, _ ByVal e As System.EventArgs) Handles Button1.Click Dim s As String = "" With Request.Browser s &= "Browser Capabilities" & vbCrLf s &= "Type = " & .Type & vbCrLf s &= "Name = " & .Browser & vbCrLf s &= "Version = " & .Version & vbCrLf s &= "Major Version = " & .MajorVersion & vbCrLf s &= "Minor Version = " & .MinorVersion & vbCrLf s &= "Platform = " & .Platform & vbCrLf s &= "Is Beta = " & .Beta & vbCrLf s &= "Is Crawler = " & .Crawler & vbCrLf s &= "Is AOL = " & .AOL & vbCrLf s &= "Is Win16 = " & .Win16 & vbCrLf s &= "Is Win32 = " & .Win32 & vbCrLf s &= "Supports Frames = " & .Frames & vbCrLf s &= "Supports Tables = " & .Tables & vbCrLf s &= "Supports Cookies = " & .Cookies & vbCrLf s &= "Supports VBScript = " & .VBScript & vbCrLf s &= "Supports JavaScript = " & _ .EcmaScriptVersion.ToString() & vbCrLf s &= "Supports Java Applets = " & .JavaApplets & vbCrLf s &= "Supports ActiveX Controls = " & .ActiveXControls & _ vbCrLf End With TextBox1.Text = s End Sub
private void Button1_Click(object sender, System.EventArgs e) { System.Web.HttpBrowserCapabilities browser = Request.Browser; string s = "Browser Capabilities\n" + "Type = " + browser.Type + "\n" + "Name = " + browser.Browser + "\n" + "Version = " + browser.Version + "\n" + "Major Version = " + browser.MajorVersion + "\n" + "Minor Version = " + browser.MinorVersion + "\n" + "Platform = " + browser.Platform + "\n" + "Is Beta = " + browser.Beta + "\n" + "Is Crawler = " + browser.Crawler + "\n" + "Is AOL = " + browser.AOL + "\n" + "Is Win16 = " + browser.Win16 + "\n" + "Is Win32 = " + browser.Win32 + "\n" + "Supports Frames = " + browser.Frames + "\n" + "Supports Tables = " + browser.Tables + "\n" + "Supports Cookies = " + browser.Cookies + "\n" + "Supports VBScript = " + browser.VBScript + "\n" + "Supports JavaScript = " + browser.EcmaScriptVersion.ToString() + "\n" + "Supports Java Applets = " + browser.JavaApplets + "\n" + "Supports ActiveX Controls = " + browser.ActiveXControls + "\n"; TextBox1.Text = s; }