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. 特別是,此範例會取得要求的 URL,從Request物件,從最常見的錯誤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.

適用於

另請參閱