HttpServerUtility Class In ASP.NET


HttpServerUtility class belongs to System.Web namespace. This class provides methods for tasks such as processing web requests and performing utility functions for encoding and decoding strings for use in URLs’. It also provides access to error information and allows changing the manner in which the current request is executed.

Methods of HttpServerUtility Class

Following are some of the important methods of the HttpServerUtility class.


This method clears the last thrown exception.


public void ClearError()

For example - Server.ClearError(); This method clears the exception that was previously thrown.


This method retrieves the last exception that was thrown.


public Exception GetLastError()

For example,

Exception lastError;

lastError is the object of class Exception. GetLastError() method retrieves the last exception that was thrown.


This method retrieves the encoded string generated by encoding the path portion of the URL string.


public string UrlPathEncode(string s) where s is the string to be encoded.

Source code

String myURL;
Response.Write(“<a href=”+Server.UrlPathEncode(myURL) + “>Hello</a>”);

Server.UrlPathEncode encodes the string myURL before sending it to the client browser

Exceptions of HttpServerUtility Class

The exception of type HttpException may occur when using certain properties and methods of the HttpServerUtility class. The table below display’s the property and method names of the HttpServerUtility class that cause the execution to be thrown and the conditions in which the exception is thrown.

ExceptionType Property/Method Name Conditions
HttpException Server.MachineName Occurs when the computer name cannot be found.
  Server.ScriptTimeout Occurs when the time-out period is a null reference. Also occurs if the time-out period cannot be set.
  Server.MapPath() method Occurs when the server object is unavailable.


The ClearError() of the HttpServerUtility class clears the last thrown exception. The StatusCode property may cause an exception of type HttpException if the status code is set after the HTTP headers have been sent.