[ Team LiB ] |
14.4 Methods Reference
Adds an Exception object to the array of exceptions returned by the AllErrors property. Parameter
ExampleThe 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 NotesAdding 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.
Clears all errors for the current request. Note that even though ClearError is singular, it clears all errors for the current request. ParametersNone. ExampleThe 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
Returns the collection of key/value pairs that are contained in the configuration specified by the name argument. Parameters
ExampleThe 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 NotesGenerally 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.
Returns the collection of key/value pairs that are contained in the configuration specified by the name argument. Parameters
ExampleThe 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 NotesGenerally, 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.
Assigns an internal rewrite path. Parameter
ExampleThe 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 NotesThis 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." |
[ Team LiB ] |