Page.Server Page.Server Page.Server Page.Server Property

定義

取得 Server 物件,這個物件為 HttpServerUtility 類別的執行個體。Gets the Server object, which is an instance of the HttpServerUtility class.

public:
 property System::Web::HttpServerUtility ^ Server { System::Web::HttpServerUtility ^ get(); };
[System.ComponentModel.Browsable(false)]
public System.Web.HttpServerUtility Server { get; }
member this.Server : System.Web.HttpServerUtility
Public ReadOnly Property Server As HttpServerUtility

屬性值

與網頁關聯的目前的 Server 物件。The current Server object associated with the page.

範例

下列程式碼範例示範如何使用Server物件,從伺服器存取錯誤資訊。The following code example demonstrates how to access error information from the server by using the Server object. 特別是,此範例會從Request物件取得要求的 URL、 Server物件中最新的錯誤(使用GetLastError方法),並將其轉換成可由用戶端顯示的字串。In particular, the example gets the requested URL from the Request object, the most recent error from the Server object (using the GetLastError method) and converts them both to strings that can be displayed by the client. message變數寫入用戶端之後,會ClearError使用方法刪除錯誤。Once the message variable is written to the client, the error is deleted using the ClearError method.

protected void Page_Error(object sender, EventArgs e)
{
  StringBuilder sb = new StringBuilder();
  sb.Append("URL that caused the error: <br/>");
  sb.Append(Server.HtmlEncode(Request.Url.ToString()));
  sb.Append("<br/><br/>");
  sb.Append("Error message: <br/>");
  sb.Append(Server.GetLastError().ToString());
  Response.Write(sb.ToString());
  Server.ClearError();
}
Protected Sub Page_Error(ByVal sender As Object, ByVal e As System.EventArgs)
  
  Dim sb As New StringBuilder()
  sb.Append("URL that caused the error: <br/>")
  sb.Append(Server.HtmlEncode(Request.Url.ToString()))
  sb.Append("<br/><br/>")
  sb.Append("Error message: <br/>")
  sb.Append(Server.GetLastError().ToString())
  Response.Write(sb.ToString())
  Server.ClearError()    

End Sub

備註

這個屬性可讓您存取經常使用HtmlEncodeMapPath和方法,以及其他方式。This property provides access to the frequently used HtmlEncode and MapPath methods, among others.

適用於

另請參閱