2012年10月1日 星期一

使用ASP.NET內建物件

ASP.NET 提供了幾個系統物件,可以用來存取許多相當有用的資訊,例如取得使用者IP,瀏覽器種類等等。 這類物件必須透過 Page 物件的屬性取得,包含:

  • Request :這個物件包含目前 HTTP 要求的相關資訊。
  • Response :這個物件包含回應資訊,允許您將資訊,透過 HTTP 回應給用戶端。
  • Context :這個物件提供網頁執行內容的相關資訊。
  • Browser :取得或設定關於用戶端瀏覽器的資訊。
  • Server :這個屬性提供常用的 HtmlEncodeMapPath 方法。
  • Application :取得目前 Web 要求的 HttpApplicationState 物件。
  • Session :這個屬性提供目前要求的工作階段(Session)資訊。
  • Trace :追蹤並顯示關於 Web 要求的執行詳細資料。

Response

Response 方法

方法說明
AddHeaderAdds an HTTP header to the response stream.
AppendCookie
AppendHeader
AppendToLog
BinaryWriteWrites binary characters to the HTTP response.
Clear
ClearContent
ClearHeaders
EndCompletes the response
Flush強迫送出目前存放在緩衝區的資料,並清除目前存放在緩衝區的資料
Redirect
SetCookie
TransmitFile
WriteWrites a text string to the HTTP response.
WriteFileWrites a file to the HTTP response with buffering.
WriteSubstitutionThis allow you to dynamically update that cached output.
Response.Redirect與Server.Transfer差別

請參考:http://www.dotblogs.com.tw/jimmyyu/archive/2009/11/10/11503.aspx

Response 屬性

屬性資料說明
BufferTrue If True, the response is buffered before sending it back to the user.
If False, the response is sent back to the user in chunks.
CacheSystem.Web.HttpCachePolicy
CookiesSystem.Web.HttpCookieCollection
Expires0
ExpiresAbsolute0001/1/1 上午 12:00:00
Filter
IsClientConnectedTrue取得瀏覽端是否還處於連線狀態
Status200 OK
StatusCode200

Request

Request 方法

方法說明
MapPath將用戶端目前要求之 URL 的虛擬路徑對應至伺服器上的實體路徑。
SaveAsSaves the request to a file.
ValidateInput是否對透過 Cookies、Form 和 QueryString 屬性執行潛在危險之驗證

Request 屬性

屬性資料說明
ApplicationPath/
AppRelativeCurrentExecutionFilePath~/C08_Programming_the_Web_Application/WebForm2.aspx取得應用程式的虛擬路徑
BrowserSystem.Web.Mobile.MobileCapabilities取得用戶端瀏覽器的資訊。
ClientCertificateSystem.Web.HttpClientCertificate
CookiesSystem.Web.HttpCookieCollection取得用戶端送出的 Cookie 的集合。
FilePath/C08_Programming_the_Web_Application/WebForm2.aspx目前要求的虛擬路徑。
FilesSystem.Web.HttpFileCollection
Filter
HeadersCache-Control=no-cache&Connection=Keep-Alive&Content-Length=429&Content-Type=application%2fx-www-form-urlencoded&Accept=text%2fhtml%2c+application%2fxhtml%2bxml%2c+*%2f*&Accept-Encoding=gzip%2c+deflate&Accept-Language=zh-TW&Cookie=ASP.NET_SessionId%3drzsgttim0kc0sagrgm4l0nvi&Host=localhost%3a60755&Referer=http%3a%2f%2flocalhost%3a60755%2fC08_Programming_the_Web_Application%2fWebForm2.aspx&User-Agent=Mozilla%2f5.0+(compatible%3b+MSIE+9.0%3b+Windows+NT+6.1%3b+WOW64%3b+Trident%2f5.0%3b+NP06)
HttpMethodPOST
IsAuthenticatedTrue
IsLocalTrue
IsSecureConnectionFalse
LogonUserIdentitySystem.Security.Principal.WindowsIdentity
Params
NameValue說明
__EVENTVALIDATION/wEWEAKy+bbNBgKf7OeoAgLw/96vBwKu+JOJCwKKkYyiBAKx6YT7DQKiotE5AveptakBAvGhyJwOApnC0/UMAu7CqaMOAvTX2fcMAuPv3sAFAqfzke4GAubYlJQIArur8Z0KILtAbiO7t33VgM69Ar+iv7lOgtaa23CBQcz9pJqpbQg=
__VIEWSTATE/wEPDwUKLTc4NzI2MDczNmQYAQUeX19Db250cm9sc1JlcXVpcmVQb3N0QmFja0tleV9fFgIFDGNiSW5jbHVkZVVybAUKY2JTaG93VGV4dGOWKpza/lXYoFZ5KMT+pWaUtalW0DTOMQ0iHhxiDaw+
ALL_HTTPHTTP_CACHE_CONTROL:no-cache
HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:420
HTTP_CONTENT_TYPE:application/x-www-form-urlencoded
HTTP_ACCEPT:text/html, application/xhtml+xml, */*
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_ACCEPT_LANGUAGE:zh-TW
HTTP_COOKIE:ASP.NET_SessionId=ent0y41l14cd4yizcegchl0o
HTTP_HOST:localhost:60755
HTTP_REFERER:http://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx
HTTP_USER_AGENT:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; NP06)
ALL_RAWCache-Control: no-cache
Connection: Keep-Alive
Content-Length: 420
Content-Type: application/x-www-form-urlencoded
Accept: text/html, application/xhtml+xml, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-TW
Cookie: ASP.NET_SessionId=ent0y41l14cd4yizcegchl0o
Host: localhost:60755
Referer: http://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; NP06)
APPL_MD_PATH
APPL_PHYSICAL_PATHD:\myMCTS\VitoPractice\70-528\Practice70515\
ASP.NET_SessionIdent0y41l14cd4yizcegchl0o
AUTH_PASSWORD
AUTH_TYPENTLM
AUTH_USERVITO-2011W7\Administrator
btnParamsRequest's Params
cbShowTexton
CERT_COOKIE
CERT_FLAGS
CERT_ISSUER
CERT_KEYSIZE
CERT_SECRETKEYSIZE
CERT_SERIALNUMBER
CERT_SERVER_ISSUER
CERT_SERVER_SUBJECT
CERT_SUBJECT
CONTENT_LENGTH420
CONTENT_TYPEapplication/x-www-form-urlencoded
GATEWAY_INTERFACE
HTTP_ACCEPTtext/html, application/xhtml+xml, */*
HTTP_ACCEPT_ENCODINGgzip, deflate
HTTP_ACCEPT_LANGUAGEzh-TW
HTTP_CACHE_CONTROLno-cache
HTTP_CONNECTIONKeep-Alive
HTTP_CONTENT_LENGTH420
HTTP_CONTENT_TYPEapplication/x-www-form-urlencoded
HTTP_COOKIEASP.NET_SessionId=ent0y41l14cd4yizcegchl0o
HTTP_HOSTlocalhost:60755
HTTP_REFERERhttp://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx
HTTP_USER_AGENTMozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; NP06)
HTTPS
HTTPS_KEYSIZE
HTTPS_SECRETKEYSIZE
HTTPS_SERVER_ISSUER
HTTPS_SERVER_SUBJECT
INSTANCE_ID
INSTANCE_META_PATH
LOCAL_ADDR::1
LOGON_USERVITO-2011W7\Administrator
PATH_INFO/C08_Programming_the_Web_Application/WebForm2.aspx
PATH_TRANSLATEDD:\myMCTS\VitoPractice\70-528\Practice70515\C08_Programming_the_Web_Application\WebForm2.aspx
QUERY_STRING
REMOTE_ADDR::1
REMOTE_HOST::1
REMOTE_PORT
REMOTE_USERVITO-2011W7\Administrator
REQUEST_METHODPOST
SCRIPT_NAME/C08_Programming_the_Web_Application/WebForm2.aspx
SERVER_NAMElocalhost
SERVER_PORT60755
SERVER_PORT_SECURE0
SERVER_PROTOCOLHTTP/1.1
SERVER_SOFTWARE
URL/C08_Programming_the_Web_Application/WebForm2.aspx
取得 QueryString、Form、Cookies 和 ServerVariables 項目的組合集合。
Path/C08_Programming_the_Web_Application/WebForm2.aspx取得目前要求的虛擬路徑。
PathInfo
PhysicalApplicationPathD:\myMCTS\VitoPractice\70-528\Practice70515\取得目前正在執行的伺服器應用程式的根目錄之實體檔案系統路徑。
PhysicalPathD:\myMCTS\VitoPractice\70-528\Practice70515\C08_Programming_the_Web_Application\WebForm2.aspx取得對應到要求的 URL 之實體檔案系統路徑。
QueryStringUser=vito&Page=2取得 HTTP 查詢字串變數的集合。
RawUrl/C08_Programming_the_Web_Application/WebForm2.aspx
ServerVariables
NameValue說明
ALL_HTTPHTTP_CACHE_CONTROL:no-cache
HTTP_CONNECTION:Keep-Alive
HTTP_CONTENT_LENGTH:438
HTTP_CONTENT_TYPE:application/x-www-form-urlencoded
HTTP_ACCEPT:text/html, application/xhtml+xml, */*
HTTP_ACCEPT_ENCODING:gzip, deflate
HTTP_ACCEPT_LANGUAGE:zh-TW
HTTP_COOKIE:ASP.NET_SessionId=ent0y41l14cd4yizcegchl0o
HTTP_HOST:localhost:60755
HTTP_REFERER:http://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx
HTTP_USER_AGENT:Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; NP06)
ALL_RAWCache-Control: no-cache
Connection: Keep-Alive
Content-Length: 438
Content-Type: application/x-www-form-urlencoded
Accept: text/html, application/xhtml+xml, */*
Accept-Encoding: gzip, deflate
Accept-Language: zh-TW
Cookie: ASP.NET_SessionId=ent0y41l14cd4yizcegchl0o
Host: localhost:60755
Referer: http://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx
User-Agent: Mozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; NP06)
APPL_MD_PATH
APPL_PHYSICAL_PATHD:\myMCTS\VitoPractice\70-528\Practice70515\
AUTH_PASSWORD
AUTH_TYPENTLM
AUTH_USERVITO-2011W7\Administrator
CERT_COOKIE
CERT_FLAGS
CERT_ISSUER
CERT_KEYSIZE
CERT_SECRETKEYSIZE
CERT_SERIALNUMBER
CERT_SERVER_ISSUER
CERT_SERVER_SUBJECT
CERT_SUBJECT
CONTENT_LENGTH438
CONTENT_TYPEapplication/x-www-form-urlencoded
GATEWAY_INTERFACE
HTTP_ACCEPTtext/html, application/xhtml+xml, */*
HTTP_ACCEPT_ENCODINGgzip, deflate
HTTP_ACCEPT_LANGUAGEzh-TW
HTTP_CACHE_CONTROLno-cache
HTTP_CONNECTIONKeep-Alive
HTTP_CONTENT_LENGTH438
HTTP_CONTENT_TYPEapplication/x-www-form-urlencoded
HTTP_COOKIEASP.NET_SessionId=ent0y41l14cd4yizcegchl0o
HTTP_HOSTlocalhost:60755
HTTP_REFERERhttp://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx
HTTP_USER_AGENTMozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; NP06)
HTTPS
HTTPS_KEYSIZE
HTTPS_SECRETKEYSIZE
HTTPS_SERVER_ISSUER
HTTPS_SERVER_SUBJECT
INSTANCE_ID
INSTANCE_META_PATH
LOCAL_ADDR::1
LOGON_USERVITO-2011W7\Administrator
PATH_INFO/C08_Programming_the_Web_Application/WebForm2.aspx
PATH_TRANSLATEDD:\myMCTS\VitoPractice\70-528\Practice70515\C08_Programming_the_Web_Application\WebForm2.aspx
QUERY_STRING
REMOTE_ADDR::1
REMOTE_HOST::1
REMOTE_PORT
REMOTE_USERVITO-2011W7\Administrator
REQUEST_METHODPOST
SCRIPT_NAME/C08_Programming_the_Web_Application/WebForm2.aspx
SERVER_NAMElocalhost
SERVER_PORT60755
SERVER_PORT_SECURE0
SERVER_PROTOCOLHTTP/1.1
SERVER_SOFTWARE
URL/C08_Programming_the_Web_Application/WebForm2.aspx
取得 Web 伺服器變數的集合。
TotalBytes429
Urlhttp://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx目前要求的 URL 資訊。
UrlReferrerhttp://localhost:60755/C08_Programming_the_Web_Application/WebForm2.aspx前一個要求之 UR 資訊。L
UserAgentMozilla/5.0 (compatible; MSIE 9.0; Windows NT 6.1; WOW64; Trident/5.0; NP06)取得用戶端瀏覽器的字串資訊
UserHostAddress::1
UserHostName::1
UserLanguagesSystem.String[]

Server

Server 方法

Server 屬性

屬性資料說明
MachineNameVITO-2011W7
ScriptTimeout30000000

Context 物件

The Page.Context property is an HttpContext object that provides access to a variety of objects related to the HTTP request and response. Many of these objects are redundant, providing access to Page members including Cache, Request, Response, Server, and Session. However, the Context object includes several unique methods:

Context 方法

方法說明
AddError
ClearError
RewritePath使用指定的路徑重寫 URL。

Context 屬性

屬性資料說明
AllErrors
IsCustomErrorEnabledTrue
IsDebuggingEnabledTrue
Timestamp2012/10/2 上午 11:52:29

Browser

取得瀏覽器相關的資訊

Request.Browser 的方法

 方法說明
AddBrowser
GetClrVersions傳回安裝在用戶端的所有 .NET Framework CLR 版本。
IsBrowser用戶端瀏覽器是否與指定瀏覽器相同。

Request.Browser 的屬性

屬性資料說明
ActiveXControlsTrue是否支援 ActiveX
AOLFalse是否是 AOL (America Online) 瀏覽器
BackgroundSoundsTrue是否支援 <bgsounds>標籤
BrowserIE
BrowsersSystem.Collections.ArrayList
ClrVersion0.0取得用戶端安裝的 .NET Framework 版本
CookiesTrue是否支援 Cookie
CrawlerFalse是否為 Web Crawler 搜尋引擎
FramesTrue是否支援 HTML 框架
IsColorTrue
IsMobileDeviceFalse
Item...
JavaAppletsTrue
JavaScriptTrue
JScriptVersion5.6
MobileDeviceManufacturerUnknown
MobileDeviceModelUnknown
MSDomVersion9.0
NumberOfSoftkeys0
PlatformWinNT
TablesTrue
VBScriptTrue
Version9.0 取得瀏覽器完整版本號碼
W3CDomVersion1.0
Win16False
Win32True

Page.Header

Page.Header 方法

Page.Header 屬性

屬性說明資料
Style取得樣式表屬性集合System.Web.UI.CssStyleCollection
StyleSheet設定網頁樣式
Style bodystyle = new Style();
bodystyle.ForeColor = System.Drawing.Color.Blue;
bodystyle.BackColor = System.Drawing.Color.LightCyan;
Page.Header.StyleSheet.CreateStyleRule(bodystyle,null,"body");

以上程式碼,將在<head>區塊裡,產生以下樣式

<style type="text/css">
	body { color:Blue;background-color:LightCyan; }
</style>
Title設定網頁標題
Page.Header.Title = "Current tim: " + DateTime.Now;
ViewStateModeInherit
VisibleTrue

沒有留言:

張貼留言