Export (0) Print
Expand All

Page.Server Property

Gets the Server object, which is an instance of the HttpServerUtility class.

[Visual Basic]
Public ReadOnly Property Server As HttpServerUtility
[C#]
public HttpServerUtility Server {get;}
[C++]
public: __property HttpServerUtility* get_Server();
[JScript]
public function get Server() : HttpServerUtility;

Property Value

The current Server object associated with the page.

Remarks

This property provides access to the frequently used HtmlEncode and MapPath methods, among others.

Example

[Visual Basic, C#, JScript] The following example demonstrates how to access error information from the server by using the Server object. 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. Once the message variable is written to the client, the error is deleted using the ClearError method.

[Visual Basic] 
Sub Page_Error(sender As Object, e As EventArgs)
    Dim message As String = "<font face=verdana color=red>" _
                           & "<h4>" & Server.HtmlEncode(Request.Url.ToString()) & "</h4>" _
                           & "<pre><font color='red'>" & Server.GetLastError().ToString() & "</pre>" _
                           & "</font>"

    Response.Write(message)
    Server.ClearError()
End Sub

[C#] 
void Page_Error(Object sender, EventArgs e) {
    String message = "<font face=verdana color=red>"
                           + "<h4>" + Server.HtmlEncode(Request.Url.ToString()) + "</h4>"
                           + "<pre><font color='red'>" + Server.GetLastError().ToString() + "</pre>"
                           + "</font>";

    Response.Write(message);
    Server.ClearError();
}

[JScript] 
function Page_Error(sender, e : EventArgs) {
    var message : String = "<font face=verdana color=red>"
                           + "<h4>" + Server.HtmlEncode(Request.Url.ToString()) + "</h4>"
                           + "<pre><font color='red'>" + Server.GetLastError().ToString() + "</pre>"
                           + "</font>";

    Response.Write(message);
    Server.ClearError();
}

[C++] No example is available for C++. To view a Visual Basic, C#, or JScript example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 2000, Windows XP Professional, Windows Server 2003 family

See Also

Page Class | Page Members | System.Web.UI Namespace | HttpServerUtility

Show:
© 2014 Microsoft