www.gibmonks.com

Main Page

  Previous Section Next Section

14.4 Methods Reference

AddError

Context.AddError(ByVal ErrorInfo As Exception)

Adds an Exception object to the array of exceptions returned by the AllErrors property.

Parameter

ErrorInfo

An Exception object to be added to the array.

Example

The example shows how you can use AddError to add an error to the current request context. The exception is created within a Try block. In the Finally block, the ToString method is used to display the error and the ClearError method is used to clear the error so that the page will display properly.

Sub Page_Load( )
    Try
        Context.AddError(New Exception("Test"))
    Finally
        Message.Text = "Context.Error.ToString( ) is " & _
           Context.Error.ToString( )
        Context.ClearError( )
    End Try
End Sub

Notes

Adding an exception by using AddError should not be confused with throwing an exception. Using AddError only adds the exception to the array returned by the AllErrors property and does not invoke any of the error handling mechanisms in the application.

ClearError

Context.ClearError( )

Clears all errors for the current request. Note that even though ClearError is singular, it clears all errors for the current request.

Parameters

None.

Example

The example checks whether there are any errors and then clears them. Finally, it reports if it has cleared any errors.

Sub Page_Load( )
   If Not Context.AllErrors Is Nothing Then
      Context.ClearError( )
      Message.Text = "Errors cleared."
   Else
      Message.Text = "No Errors to clear."
   End If
End Sub
GetAppConfig

Object = HttpContext.GetAppConfig(ByVal name As String)

Returns the collection of key/value pairs that are contained in the configuration specified by the name argument.

Parameters

Object

An object containing the keys and values in the configuration sections specified by name. This object is often of a type derived from NameValueCollection.

name

The name of the section to retrieve.

Example

The example shows how you can use the GetAppConfig method to retrieve all items in a configuration by setting from the web.config or machine.config XML configuration file. While GetAppConfig returns an Object, you must cast the returned object to the NameValueCollection-derived type defined in the configuration section to actually access the information. This method is static, so an instance of the HttpContext class is not required.

Sub Page_Load( )
   Dim i As Integer
   Dim nv As NameValueCollection
   nv = CType(HttpContext.GetAppConfig("appSettings"), _
              NameValueCollection)
   For i = 0 To nv.Count - 1
       Response.Write(nv.GetKey(i) & " = " & nv(i) & "<br/>")
   Next         
End Sub

Notes

Generally you will not use GetAppConfig to get the appSettings section from the configuration file. It is much easier and safer to use ConfigurationSettings.AppSettings to get at these values. This method, however, can be used to get information from custom configuration sections.

GetConfig

Object = Context.GetConfig(ByVal name As String)

Returns the collection of key/value pairs that are contained in the configuration specified by the name argument.

Parameters

Object

An object containing the keys and values in the configuration sections specified by name. This object is often of a type derived from NameValueCollection.

name

The name of the section to retrieve.

Example

The example shows how you can use the GetConfig method to retrieve all items in a configuration setting from the web.config or machine.config XML configuration file. While GetAppConfig returns an object, you must cast the returned object to the NameValueCollection-derived type defined in the configuration section to actually access the information.

Sub Page_Load( )
   Dim i As Integer
   Dim nv As NameValueCollection
   nv = CType(Context.GetConfig("appSettings"), _
              NameValueCollection)
   For i = 0 To nv.Count - 1
      Response.Write(nv.GetKey(i) & " = " & nv(i) & "<br/>")
   Next         
End Sub

Notes

Generally, you will not use GetConfig to get the appSettings section from the configuration file. It is much easier and safer to use ConfigurationSettings.AppSettings to get these values. This method, however, can be used to get information from custom configuration sections.

RewritePath

Context.RewritePath(ByVal newURL As String)

Assigns an internal rewrite path.

Parameter

newURL

A String containing a local path to redirect the user silently.

Example

The example below shows how to change the path in a way that is completely transparent to the user. The URL shown in the address bar remains the original URL, and the redirection to the new page does not require a round trip to and from the server. RewritePath is almost always called from global.asax rather than an actual page. That is what this example shows.

Sub Application_BeginRequest(ByVal sender As Object, _ 
   ByVal e As EventArgs)
   ' No matter the URL, redirect to this URL...
   Context.RewritePath("/aspnetian/ShowGetConfig.aspx")
End Sub

Notes

This method seems to be redundant when compared with methods like Server.Transfer, which allow the developer to change the page being displayed. In fact, RewritePath serves a very unique purpose.

Perhaps you have seen or registered at Web sites that give registered users a unique URL. For instance, you might be given a URL like this:

http://www.SomeDomain.com/YourName/default.aspx

Implementing such a system that gives a virtual directory to each user is not practical unless you have very few registered users. Using RewritePath, the developer can essentially remove a level of directory hierarchy. This removal occurs without a redirect that would require a round trip to the server, and without changing the URL as it appears in the browser. Some information will be extracted and saved in the Context Items collection for use by pages that will be displayed while translating from the URL entered by the user to the URL used for RewritePath.

In the example above, RewritePath might be sent a URL like this:

http://www.SomeDomain.com/default.aspx

The "YourName" folder name was removed from the URL, and the application can then be customized for the user identified as "YourName."

      Previous Section Next Section