Work with ASP.NET Intrinsic Objects

There are a bunch of objects built into ASP.NET for providing information about the current state of the server, application, response, etc. These objects are called the ASP.NET Intrinsic Objects. You can gain access to many of them through the Page object. The Intrinsic Objects are:

  • Context
  • Application
  • Request
  • Server
  • Response
  • Session

Context:

Context is an HttpContext object that provides information about the request and response of the current page. Many objects here are providing access for various other intrinsic objects, but here are some exceptions:

Context Methods
AddError Adds an exception to the page (like throwing one). It can be retrieved later by calling Server.GetLastError, or simply be cleared.
ClearError Clears the last exception, just like Server.ClearError.
RewritePath Assigns an internal path and allows the URL to differ from it.
AllErrors An error collection unhandled in the current page.
IsCustomErrorEnabled Returns true if custom errors are enabled.
IsDebuggingEnabled Returns true if debugging is enabled.
TimeStamp The timestamp of the request.

You can gain access to the current context by using the static property “Current” of the HttpContext class. This is useful if you need information of the current context in an object which doesn’t derive from Page. The syntax you’d use is the following:

HttpContext current = HttpContext.Current;
current.Server.ClearError();

Application:

The Application object enables you to add, retrieve and inspect the content of the Application state. It also has some events specified in this post, which you can access by a global.asax file.

Request:

Request is the object that wraps the information of the current request made by the user. It is an instance of the HttpRequest class. It comes handy when you want to examine the user’s browser, or the cookies sent to the server.

Request
MapPath This method enabled you to convert the request URL into a file path to the requested resource.
SaveAs Saves the request to a file.
ValidateInput This method is useful if you’ve disabled input validation. It throws an exception if the user input contains dangerous data.
Browser Gets information about the client’s browser. More info later.
Cookies A collection of cookies sent by the client.
Files A collection of uploaded files by the client (if any).
Headers A collection of HTTP headers.
HttpMethod The type of the HTTP verb used by the client.
IsAuthenticated Returns true if the user is authenticated.
IsLocal Returns true if the client is a local server client.
IsSecureConnection Returns true if the connection uses SSL.
Params A combined collection of query strings, server variables, form data and cookies.
QueryString A collection of query string values.
RawUrl and Url The URL of the current request.
UserAgent Gets the user agent.
UserHostAddress The IP of the current user.
UserHostName The DNS of the current user.
UserLanguages A collection of preferred languages of the browser.

To determine the user’s browser capabilities you should use Request.Browser, which return an HttpBrowserCapabilities object. The HttpBrowserCapabilities object has the following methods and properties:

  • IsBrowser – takes a string value and returns true if the current browser is the browser specified in that string.
  • GetClrVersions – returns all installed versions of the .NET Framework Common Language Runtime.
  • ActiveXControls – returns true if the browser supports ActiveX controls.
  • Browser – Gets the user agent string.
  • ClrVersion – gets the installed CLR version of the client.
  • Cookies – returns true if the current browser supports cookies.
  • Crawler – indicates whether the current browser is a search engine crawler robot.
  • Frames – indicates whether the current browser supports frames.
  • IsColor – indicates whether the current browser supports colors. False values typically mean mobile devices.
  • IsMobileDevice – indicates whether the current browser is a mobile device.
  • JavaScript – returns true if the browser supports JavaScript.
  • JScriptVersion – gets the version of JScript the browser supports.
  • MobileDeviceManufacturer/MobileDeviceName – gets the info if available.
  • Tables – gets a value indicating whether the browser supports table HTML elements.
  • Version: gets the full version of the browser in a string format.

 

Server:

The server object is an instance of the HtppServerUtility class. It provides static methods for URL and HTML encoding and decoding, and for modifying paths.

Server Methods
ClearError Deletes the last error occurred.
GetLastError Gets the last error occurred on the server.
HtmlDecode Removes HTML markup from a string. This method should be called on user input to prevent attacks.
HtmlEncode Converts a string to be displayed in a browser.
MapPath Returns the physical file path of the specified virtual path.
Transfer Starts processing the specified page without changing the URL in the browser.
UrlDecode Decodes a URL string into normalized form. No %20s.
UrlEncode Prepares a string to be a part of the URL. This means %20s.

 

Response:

Response is an instance of HttpResponse object that represents the server’s response to the client browser. It allows you to modify content, or to redirect a user. Response.Redirect involves a round-trip to the server (which means a Browser(Request) – Server(Process) – Browser(Request new URL) – Server(Process)). As a workaround, use Server.Transfer. A response is built from two ingredients: headers and content. You can modify both of them with Response. You can also add additional cookies to the client here.

Response
AppendHeader Adds an HTTP header to the output.
Clear Clears everything from the response stream.
ClearContent/ClearHeaders Clears just the content, headers of the response stream.
End Ends the response and returns the page to the user.
Flush Sends the current output to the user, without finishing it. Use it when time-consuming tasks occur.
Redirect Redirects the user to another page, which requires a round-trip to the server. The page can be anywhere.
TransmitFile Writes a file to the response without buffering it.
WriteFile Writes a file to the response with buffering it.
Write Writes text to the response with buffering.

 

Session:

Session object is an instance of HtppSessionState class. It stores data in a user-scope which needs to be persisted between page-requests. Session uses a dictionary-styled (key-value pairs) collection to store its data. To access Session events, you should use the global.asax file.

Session
Add Adds a string-based name and an object-based value to the user’s session
Abandon Cancels the current session. Calls the Session_End event.
Session.CookieMode Gets the cookie mode of the current session.
IsNewSession Gets a value that indicates whether the session is started at the current request.
Mode Gets the current session state mode.
SessionID Gets the ID of the current session.

 

Further Readings:

HttpApplication Members
HttpContext Members
HttpResponse Members
HttpRequest Members
HttpServerUtility Members
HttpSessionState Members

Advertisements

Tags: , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

w

Connecting to %s

%d bloggers like this: