2017年3月4日 星期六

[研究][C#][ASP.NET][WebForm] 偵測瀏覽器類型(二)

[研究][C#][ASP.NET][WebForm] 偵測瀏覽器類型(二)

2017-03-04

Visual Studio 2015 with Update 3

因為 HttpBrowserCapabilities 類別還有很多屬性,所以寫這篇紀錄測試。

參考

HttpBrowserCapabilities 類別
https://msdn.microsoft.com/zh-tw/library/system.web.httpbrowsercapabilities(v=vs.110).aspx

偵測 Web Form 中的瀏覽器類型
https://msdn.microsoft.com/zh-tw/library/cc438041(v=vs.71).aspx

HOW TO:在 ASP.NET Web 網頁中偵測瀏覽器類型
https://msdn.microsoft.com/zh-tw/library/3yekbd5b(v=vs.100).aspx

Default.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="HttpBrowserCapabilitiesDemo.Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    </div>
    </form>
</body>
</html>



Default.aspx.cs
using System;

namespace HttpBrowserCapabilitiesDemo
{
    public partial class Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            System.Web.HttpBrowserCapabilities browser = Request.Browser;
            string s = "Browser Capabilities<br /><br />"
+ "ActiveXControls = " + browser.ActiveXControls + "<br />"
+ "Adapters = " + browser.Adapters + "<br />"
+ "AOL = " + browser.AOL + "<br />"
+ "BackgroundSounds = " + browser.BackgroundSounds + "<br />"
+ "Beta = " + browser.Beta + "<br />"
+ "Browser = " + browser.Browser + "<br />"
+ "Browsers = " + browser.Browsers + "<br />"
+ "CanCombineFormsInDeck = " + browser.CanCombineFormsInDeck + "<br />"
+ "CanInitiateVoiceCall = " + browser.CanInitiateVoiceCall + "<br />"
+ "CanRenderAfterInputOrSelectElement = " + browser.CanRenderAfterInputOrSelectElement + "<br />"
+ "CanRenderEmptySelects = " + browser.CanRenderEmptySelects + "<br />"
+ "CanRenderInputAndSelectElementsTogether = " + browser.CanRenderInputAndSelectElementsTogether + "<br />"
+ "CanRenderMixedSelects = " + browser.CanRenderMixedSelects + "<br />"
+ "CanRenderOneventAndPrevElementsTogether = " + browser.CanRenderOneventAndPrevElementsTogether + "<br />"
+ "CanRenderPostBackCards = " + browser.CanRenderPostBackCards + "<br />"
+ "CanRenderSetvarZeroWithMultiSelectionList = " + browser.CanRenderSetvarZeroWithMultiSelectionList + "<br />"
+ "CanSendMail = " + browser.CanSendMail + "<br />"
+ "Capabilities = " + browser.Capabilities + "<br />"
+ "CDF = " + browser.CDF + "<br />"
+ "ClrVersion = " + browser.ClrVersion + "<br />"
+ "Cookies = " + browser.Cookies + "<br />"
+ "Crawler = " + browser.Crawler + "<br />"
+ "DefaultSubmitButtonLimit = " + browser.DefaultSubmitButtonLimit + "<br />"
+ "EcmaScriptVersion = " + browser.EcmaScriptVersion + "<br />"
+ "Frames = " + browser.Frames + "<br />"
+ "GatewayMajorVersion = " + browser.GatewayMajorVersion + "<br />"
+ "GatewayMinorVersion = " + browser.GatewayMinorVersion + "<br />"
+ "GatewayVersion = " + browser.GatewayVersion + "<br />"
+ "HasBackButton = " + browser.HasBackButton + "<br />"
+ "HidesRightAlignedMultiselectScrollbars = " + browser.HidesRightAlignedMultiselectScrollbars + "<br />"
+ "HtmlTextWriter = " + browser.HtmlTextWriter + "<br />"
+ "Id = " + browser.Id + "<br />"
+ "InputType = " + browser.InputType + "<br />"
+ "IsColor = " + browser.IsColor + "<br />"
+ "IsMobileDevice = " + browser.IsMobileDevice + "<br />"
//+ "Item[String] = " + browser.Item[String] + "<br />"
+ "Item[String] = " + "略" + "<br />"
+ "JavaApplets = " + browser.JavaApplets + "<br />"
+ "JavaScript = " + browser.JavaScript + "<br />"
+ "JScriptVersion = " + browser.JScriptVersion + "<br />"
+ "MajorVersion = " + browser.MajorVersion + "<br />"
+ "MaximumHrefLength = " + browser.MaximumHrefLength + "<br />"
+ "MaximumRenderedPageSize = " + browser.MaximumRenderedPageSize + "<br />"
+ "MaximumSoftkeyLabelLength = " + browser.MaximumSoftkeyLabelLength + "<br />"
+ "MinorVersion = " + browser.MinorVersion + "<br />"
+ "MinorVersionString = " + browser.MinorVersionString + "<br />"
+ "MobileDeviceManufacturer = " + browser.MobileDeviceManufacturer + "<br />"
+ "MobileDeviceModel = " + browser.MobileDeviceModel + "<br />"
+ "MSDomVersion = " + browser.MSDomVersion + "<br />"
+ "NumberOfSoftkeys = " + browser.NumberOfSoftkeys + "<br />"
+ "Platform = " + browser.Platform + "<br />"
+ "PreferredImageMime = " + browser.PreferredImageMime + "<br />"
+ "PreferredRenderingMime = " + browser.PreferredRenderingMime + "<br />"
+ "PreferredRenderingType = " + browser.PreferredRenderingType + "<br />"
+ "PreferredRequestEncoding = " + browser.PreferredRequestEncoding + "<br />"
+ "PreferredResponseEncoding = " + browser.PreferredResponseEncoding + "<br />"
+ "RendersBreakBeforeWmlSelectAndInput = " + browser.RendersBreakBeforeWmlSelectAndInput + "<br />"
+ "RendersBreaksAfterHtmlLists = " + browser.RendersBreaksAfterHtmlLists + "<br />"
+ "RendersBreaksAfterWmlAnchor = " + browser.RendersBreaksAfterWmlAnchor + "<br />"
+ "RendersBreaksAfterWmlInput = " + browser.RendersBreaksAfterWmlInput + "<br />"
+ "RendersWmlDoAcceptsInline = " + browser.RendersWmlDoAcceptsInline + "<br />"
+ "RendersWmlSelectsAsMenuCards = " + browser.RendersWmlSelectsAsMenuCards + "<br />"
+ "RequiredMetaTagNameValue = " + browser.RequiredMetaTagNameValue + "<br />"
+ "RequiresAttributeColonSubstitution = " + browser.RequiresAttributeColonSubstitution + "<br />"
+ "RequiresContentTypeMetaTag = " + browser.RequiresContentTypeMetaTag + "<br />"
+ "RequiresControlStateInSession = " + browser.RequiresControlStateInSession + "<br />"
+ "RequiresDBCSCharacter = " + browser.RequiresDBCSCharacter + "<br />"
+ "RequiresHtmlAdaptiveErrorReporting = " + browser.RequiresHtmlAdaptiveErrorReporting + "<br />"
+ "RequiresLeadingPageBreak = " + browser.RequiresLeadingPageBreak + "<br />"
+ "RequiresNoBreakInFormatting = " + browser.RequiresNoBreakInFormatting + "<br />"
+ "RequiresOutputOptimization = " + browser.RequiresOutputOptimization + "<br />"
+ "RequiresPhoneNumbersAsPlainText = " + browser.RequiresPhoneNumbersAsPlainText + "<br />"
+ "RequiresSpecialViewStateEncoding = " + browser.RequiresSpecialViewStateEncoding + "<br />"
+ "RequiresUniqueFilePathSuffix = " + browser.RequiresUniqueFilePathSuffix + "<br />"
+ "RequiresUniqueHtmlCheckboxNames = " + browser.RequiresUniqueHtmlCheckboxNames + "<br />"
+ "RequiresUniqueHtmlInputNames = " + browser.RequiresUniqueHtmlInputNames + "<br />"
+ "RequiresUrlEncodedPostfieldValues = " + browser.RequiresUrlEncodedPostfieldValues + "<br />"
+ "ScreenBitDepth = " + browser.ScreenBitDepth + "<br />"
+ "ScreenCharactersHeight = " + browser.ScreenCharactersHeight + "<br />"
+ "ScreenCharactersWidth = " + browser.ScreenCharactersWidth + "<br />"
+ "ScreenPixelsHeight = " + browser.ScreenPixelsHeight + "<br />"
+ "ScreenPixelsWidth = " + browser.ScreenPixelsWidth + "<br />"
+ "SupportsAccesskeyAttribute = " + browser.SupportsAccesskeyAttribute + "<br />"
+ "SupportsBodyColor = " + browser.SupportsBodyColor + "<br />"
+ "SupportsBold = " + browser.SupportsBold + "<br />"
+ "SupportsCacheControlMetaTag = " + browser.SupportsCacheControlMetaTag + "<br />"
+ "SupportsCallback = " + browser.SupportsCallback + "<br />"
+ "SupportsCss = " + browser.SupportsCss + "<br />"
+ "SupportsDivAlign = " + browser.SupportsDivAlign + "<br />"
+ "SupportsDivNoWrap = " + browser.SupportsDivNoWrap + "<br />"
+ "SupportsEmptyStringInCookieValue = " + browser.SupportsEmptyStringInCookieValue + "<br />"
+ "SupportsFontColor = " + browser.SupportsFontColor + "<br />"
+ "SupportsFontName = " + browser.SupportsFontName + "<br />"
+ "SupportsFontSize = " + browser.SupportsFontSize + "<br />"
+ "SupportsImageSubmit = " + browser.SupportsImageSubmit + "<br />"
+ "SupportsIModeSymbols = " + browser.SupportsIModeSymbols + "<br />"
+ "SupportsInputIStyle = " + browser.SupportsInputIStyle + "<br />"
+ "SupportsInputMode = " + browser.SupportsInputMode + "<br />"
+ "SupportsItalic = " + browser.SupportsItalic + "<br />"
+ "SupportsJPhoneMultiMediaAttributes = " + browser.SupportsJPhoneMultiMediaAttributes + "<br />"
+ "SupportsJPhoneSymbols = " + browser.SupportsJPhoneSymbols + "<br />"
+ "SupportsQueryStringInFormAction = " + browser.SupportsQueryStringInFormAction + "<br />"
+ "SupportsRedirectWithCookie = " + browser.SupportsRedirectWithCookie + "<br />"
+ "SupportsSelectMultiple = " + browser.SupportsSelectMultiple + "<br />"
+ "SupportsUncheck = " + browser.SupportsUncheck + "<br />"
+ "SupportsXmlHttp = " + browser.SupportsXmlHttp + "<br />"
+ "Tables = " + browser.Tables + "<br />"
+ "TagWriter = " + browser.TagWriter + "<br />"
+ "Type = " + browser.Type + "<br />"
+ "UseOptimizedCacheKey = " + browser.UseOptimizedCacheKey + "<br />"
+ "VBScript = " + browser.VBScript + "<br />"
+ "Version = " + browser.Version + "<br />"
+ "W3CDomVersion = " + browser.W3CDomVersion + "<br />"
+ "Win16 = " + browser.Win16 + "<br />"
+ "Win32 = " + browser.Win32 + "<br />";

            Label1.Text = s;
        }
    }
}

編譯有警告,先不理會

            // 警告  CS0618  'HttpCapabilitiesBase.JavaScript' 已經過時: 'The recommended alternative is the EcmaScriptVersion property. A Major version value greater than or equal to 1 implies JavaScript support. http://go.microsoft.com/fwlink/?linkid=14202'  Browser D:\CodeTemp\Browser\Browser\Default.aspx.cs 28  作用中

執行結果範例



Windows 7 x64 上 IE11, Chrome 56, FireFox 51.0.1 (64位元) 瀏覽器測試結果
//****************************************
Browser Capabilities

ActiveXControls = False
Adapters = System.Collections.Hashtable
AOL = False
BackgroundSounds = False
Beta = False
Browser = InternetExplorer
Browsers = System.Collections.ArrayList
CanCombineFormsInDeck = True
CanInitiateVoiceCall = False
CanRenderAfterInputOrSelectElement = True
CanRenderEmptySelects = True
CanRenderInputAndSelectElementsTogether = True
CanRenderMixedSelects = True
CanRenderOneventAndPrevElementsTogether = True
CanRenderPostBackCards = True
CanRenderSetvarZeroWithMultiSelectionList = True
CanSendMail = True
Capabilities = System.Collections.Hashtable
CDF = False
ClrVersion = 0.0
Cookies = True
Crawler = False
DefaultSubmitButtonLimit = 1
EcmaScriptVersion = 3.0
Frames = True
GatewayMajorVersion = 0
GatewayMinorVersion = 0
GatewayVersion = None
HasBackButton = True
HidesRightAlignedMultiselectScrollbars = False
HtmlTextWriter =
Id = internetexplorer
InputType = keyboard
IsColor = True
IsMobileDevice = False
Item[String] = 略
JavaApplets = False
JavaScript = True
JScriptVersion = 0.0
MajorVersion = 11
MaximumHrefLength = 10000
MaximumRenderedPageSize = 300000
MaximumSoftkeyLabelLength = 5
MinorVersion = 0
MinorVersionString = 0
MobileDeviceManufacturer = Unknown
MobileDeviceModel = Unknown
MSDomVersion = 0.0
NumberOfSoftkeys = 0
Platform = WinNT
PreferredImageMime = image/gif
PreferredRenderingMime = text/html
PreferredRenderingType = html32
PreferredRequestEncoding =
PreferredResponseEncoding =
RendersBreakBeforeWmlSelectAndInput = False
RendersBreaksAfterHtmlLists = True
RendersBreaksAfterWmlAnchor = False
RendersBreaksAfterWmlInput = False
RendersWmlDoAcceptsInline = True
RendersWmlSelectsAsMenuCards = False
RequiredMetaTagNameValue =
RequiresAttributeColonSubstitution = False
RequiresContentTypeMetaTag = False
RequiresControlStateInSession = False
RequiresDBCSCharacter = False
RequiresHtmlAdaptiveErrorReporting = False
RequiresLeadingPageBreak = False
RequiresNoBreakInFormatting = False
RequiresOutputOptimization = False
RequiresPhoneNumbersAsPlainText = False
RequiresSpecialViewStateEncoding = False
RequiresUniqueFilePathSuffix = False
RequiresUniqueHtmlCheckboxNames = False
RequiresUniqueHtmlInputNames = False
RequiresUrlEncodedPostfieldValues = False
ScreenBitDepth = 8
ScreenCharactersHeight = 40
ScreenCharactersWidth = 80
ScreenPixelsHeight = 480
ScreenPixelsWidth = 640
SupportsAccesskeyAttribute = False
SupportsBodyColor = True
SupportsBold = True
SupportsCacheControlMetaTag = True
SupportsCallback = True
SupportsCss = True
SupportsDivAlign = True
SupportsDivNoWrap = True
SupportsEmptyStringInCookieValue = True
SupportsFontColor = True
SupportsFontName = True
SupportsFontSize = True
SupportsImageSubmit = True
SupportsIModeSymbols = False
SupportsInputIStyle = False
SupportsInputMode = False
SupportsItalic = True
SupportsJPhoneMultiMediaAttributes = False
SupportsJPhoneSymbols = False
SupportsQueryStringInFormAction = True
SupportsRedirectWithCookie = True
SupportsSelectMultiple = True
SupportsUncheck = True
SupportsXmlHttp = True
Tables = True
TagWriter = System.Web.UI.HtmlTextWriter
Type = InternetExplorer11
UseOptimizedCacheKey = True
VBScript = False
Version = 11.0
W3CDomVersion = 1.0
Win16 = False
Win32 = True
 
****************************************
Browser Capabilities

ActiveXControls = False
Adapters = System.Collections.Hashtable
AOL = False
BackgroundSounds = False
Beta = False
Browser = Chrome
Browsers = System.Collections.ArrayList
CanCombineFormsInDeck = True
CanInitiateVoiceCall = False
CanRenderAfterInputOrSelectElement = True
CanRenderEmptySelects = True
CanRenderInputAndSelectElementsTogether = True
CanRenderMixedSelects = True
CanRenderOneventAndPrevElementsTogether = True
CanRenderPostBackCards = True
CanRenderSetvarZeroWithMultiSelectionList = True
CanSendMail = True
Capabilities = System.Collections.Hashtable
CDF = False
ClrVersion = 0.0
Cookies = True
Crawler = False
DefaultSubmitButtonLimit = 1
EcmaScriptVersion = 3.0
Frames = True
GatewayMajorVersion = 0
GatewayMinorVersion = 0
GatewayVersion = None
HasBackButton = True
HidesRightAlignedMultiselectScrollbars = False
HtmlTextWriter =
Id = chrome
InputType = keyboard
IsColor = True
IsMobileDevice = False
Item[String] = 略
JavaApplets = True
JavaScript = True
JScriptVersion = 0.0
MajorVersion = 56
MaximumHrefLength = 10000
MaximumRenderedPageSize = 300000
MaximumSoftkeyLabelLength = 5
MinorVersion = 0
MinorVersionString = 0
MobileDeviceManufacturer = Unknown
MobileDeviceModel = Unknown
MSDomVersion = 0.0
NumberOfSoftkeys = 0
Platform = WinNT
PreferredImageMime = image/gif
PreferredRenderingMime = text/html
PreferredRenderingType = html32
PreferredRequestEncoding =
PreferredResponseEncoding =
RendersBreakBeforeWmlSelectAndInput = False
RendersBreaksAfterHtmlLists = True
RendersBreaksAfterWmlAnchor = False
RendersBreaksAfterWmlInput = False
RendersWmlDoAcceptsInline = True
RendersWmlSelectsAsMenuCards = False
RequiredMetaTagNameValue =
RequiresAttributeColonSubstitution = False
RequiresContentTypeMetaTag = False
RequiresControlStateInSession = False
RequiresDBCSCharacter = False
RequiresHtmlAdaptiveErrorReporting = False
RequiresLeadingPageBreak = False
RequiresNoBreakInFormatting = False
RequiresOutputOptimization = False
RequiresPhoneNumbersAsPlainText = False
RequiresSpecialViewStateEncoding = False
RequiresUniqueFilePathSuffix = False
RequiresUniqueHtmlCheckboxNames = False
RequiresUniqueHtmlInputNames = False
RequiresUrlEncodedPostfieldValues = False
ScreenBitDepth = 8
ScreenCharactersHeight = 40
ScreenCharactersWidth = 80
ScreenPixelsHeight = 480
ScreenPixelsWidth = 640
SupportsAccesskeyAttribute = True
SupportsBodyColor = True
SupportsBold = True
SupportsCacheControlMetaTag = True
SupportsCallback = True
SupportsCss = True
SupportsDivAlign = True
SupportsDivNoWrap = False
SupportsEmptyStringInCookieValue = True
SupportsFontColor = True
SupportsFontName = True
SupportsFontSize = True
SupportsImageSubmit = True
SupportsIModeSymbols = False
SupportsInputIStyle = False
SupportsInputMode = False
SupportsItalic = True
SupportsJPhoneMultiMediaAttributes = False
SupportsJPhoneSymbols = False
SupportsQueryStringInFormAction = True
SupportsRedirectWithCookie = True
SupportsSelectMultiple = True
SupportsUncheck = True
SupportsXmlHttp = True
Tables = True
TagWriter = System.Web.UI.HtmlTextWriter
Type = Chrome56
UseOptimizedCacheKey = True
VBScript = False
Version = 56.0
W3CDomVersion = 1.0
Win16 = False
Win32 = True
****************************************

Browser Capabilities

ActiveXControls = False
Adapters = System.Collections.Hashtable
AOL = False
BackgroundSounds = False
Beta = False
Browser = Firefox
Browsers = System.Collections.ArrayList
CanCombineFormsInDeck = True
CanInitiateVoiceCall = False
CanRenderAfterInputOrSelectElement = True
CanRenderEmptySelects = True
CanRenderInputAndSelectElementsTogether = True
CanRenderMixedSelects = True
CanRenderOneventAndPrevElementsTogether = True
CanRenderPostBackCards = True
CanRenderSetvarZeroWithMultiSelectionList = True
CanSendMail = True
Capabilities = System.Collections.Hashtable
CDF = False
ClrVersion = 0.0
Cookies = True
Crawler = False
DefaultSubmitButtonLimit = 1
EcmaScriptVersion = 3.0
Frames = True
GatewayMajorVersion = 0
GatewayMinorVersion = 0
GatewayVersion = None
HasBackButton = True
HidesRightAlignedMultiselectScrollbars = False
HtmlTextWriter =
Id = firefox3plus
InputType = keyboard
IsColor = True
IsMobileDevice = False
Item[String] = 略
JavaApplets = True
JavaScript = True
JScriptVersion = 0.0
MajorVersion = 51
MaximumHrefLength = 10000
MaximumRenderedPageSize = 300000
MaximumSoftkeyLabelLength = 5
MinorVersion = 0
MinorVersionString = 0
MobileDeviceManufacturer = Unknown
MobileDeviceModel = Unknown
MSDomVersion = 0.0
NumberOfSoftkeys = 0
Platform = WinNT
PreferredImageMime = image/gif
PreferredRenderingMime = text/html
PreferredRenderingType = html32
PreferredRequestEncoding =
PreferredResponseEncoding =
RendersBreakBeforeWmlSelectAndInput = False
RendersBreaksAfterHtmlLists = True
RendersBreaksAfterWmlAnchor = False
RendersBreaksAfterWmlInput = False
RendersWmlDoAcceptsInline = True
RendersWmlSelectsAsMenuCards = False
RequiredMetaTagNameValue =
RequiresAttributeColonSubstitution = False
RequiresContentTypeMetaTag = False
RequiresControlStateInSession = False
RequiresDBCSCharacter = False
RequiresHtmlAdaptiveErrorReporting = False
RequiresLeadingPageBreak = False
RequiresNoBreakInFormatting = False
RequiresOutputOptimization = False
RequiresPhoneNumbersAsPlainText = False
RequiresSpecialViewStateEncoding = False
RequiresUniqueFilePathSuffix = False
RequiresUniqueHtmlCheckboxNames = False
RequiresUniqueHtmlInputNames = False
RequiresUrlEncodedPostfieldValues = False
ScreenBitDepth = 8
ScreenCharactersHeight = 40
ScreenCharactersWidth = 80
ScreenPixelsHeight = 480
ScreenPixelsWidth = 640
SupportsAccesskeyAttribute = True
SupportsBodyColor = True
SupportsBold = True
SupportsCacheControlMetaTag = True
SupportsCallback = True
SupportsCss = True
SupportsDivAlign = True
SupportsDivNoWrap = False
SupportsEmptyStringInCookieValue = True
SupportsFontColor = True
SupportsFontName = True
SupportsFontSize = True
SupportsImageSubmit = True
SupportsIModeSymbols = False
SupportsInputIStyle = False
SupportsInputMode = False
SupportsItalic = True
SupportsJPhoneMultiMediaAttributes = False
SupportsJPhoneSymbols = False
SupportsQueryStringInFormAction = True
SupportsRedirectWithCookie = True
SupportsSelectMultiple = True
SupportsUncheck = True
SupportsXmlHttp = True
Tables = True
TagWriter = System.Web.UI.HtmlTextWriter
Type = Firefox51
UseOptimizedCacheKey = True
VBScript = False
Version = 51.0
W3CDomVersion = 1.0
Win16 = False
Win32 = True

****************************************

(完)

相關

[研究][C#][ASP.NET][WebForm] 偵測瀏覽器類型(二)
http://shaurong.blogspot.com/2017/03/caspnetwebform.html

[研究][C#][ASP.NET][WebForm] 偵測瀏覽器類型(一)
http://shaurong.blogspot.com/2017/01/caspnet.html

[研究] 檢查瀏覽器 JavaScript是否支援?是否啟用?
http://shaurong.blogspot.com/2016/01/aspnetc-javascript.html


沒有留言:

張貼留言