Online Book Reader

Home Category

Programming Microsoft ASP.NET 4 - Dino Esposito [337]

By Root 5689 0
= HttpContext.Current;

var o = context.Request["id"];

if (o != null)

{

var id = (Int32) o;

var url = GetPageUrlFromId(id);

context.RewritePath(url);

}

}

protected String GetPageUrlFromId(Int32 id)

{

// Return a full URL based on the input ID value.

...

}

The preceding code rewrites a URL such as page.aspx?id=1234 to a specific page whose real URL is read out of a database or a configuration file.

Note

In general, IIS-level URL rewriting (which was discussed in Chapter 2) is a better alternative. The newer and more general ASP.NET Routing is perhaps better suited for a more complex use case, but it can achieve the same result pretty easily.

Loading Resources Programmatically


In Chapter 7, we discussed expressions allowed in ASP.NET pages to bind control properties to embedded global or local resources. The $Resources and meta:resourcekey expressions for global and local resources, respectively, work only at design time. What if you need to generate text programmatically that embeds resource expressions, instead? Both the Page and HttpContext classes support a pair of programmatic methods to retrieve the content of resources embedded in the application.

GetGlobalResourceObject retrieves a global resource—that is, a resource defined in an .resx file located in the App_GlobalResources special folder. GetLocalResourceObject does the same for an .resx file located in the App_LocalResources special folder of a given page.

msg1.Text = (String) HttpContext.GetGlobalResourceObject(

"Test", "MyString");

msg2.Text = (String) HttpContext.GetLocalResourceObject(

"/MyApp/Samples/ResPage.aspx", "PageResource1.Title");

The first parameter you pass to GetGlobalResourceObject indicates the name of the .resx resource file without an extension; the second parameter is the name of the resource to retrieve. As for GetLocalResourceObject, the first argument indicates the virtual path of the page; the second is the name of the resource.

The Server Object


In the all-encompassing container represented by the HttpContext object, a few popular objects also find their place. Among them are Server, Request, and Response. They are old acquaintances for ASP developers and, indeed, they are feature-rich elements of the ASP.NET programming toolkit. The set of properties and methods still makes these objects a fundamental resource for developers. Let’s learn more about them, starting with the Server object.

The functionality of the ASP intrinsic Server object in ASP.NET is implemented by the HttpServerUtility class. An instance of the type is created when ASP.NET begins to process the request and is then stored as part of the request context. The bunch of helper methods that HttpServerUtility provides are publicly exposed to modules and handlers—including global.asax, pages, and Web services—through the Server property of the HttpContext object. In addition, to maintain ASP.NET coding as close as possible to the ASP programming style, several other commonly used ASP.NET objects also expose their own Server property. In this way, developers can use in the code, say, Server.MapPath without incurring compile errors.

Properties of the HttpServerUtility Class


This class provides two properties, named MachineName and ScriptTimeout. The MachineName property returns the machine name, whereas ScriptTimeout gets and sets the time in seconds that a request is allowed to be processed. This property accepts integers and defaults to 90 seconds; however, it is set to a virtually infinite value if the page runs with the attribute debug=true, as shown here:

this.Server.ScriptTimeout = 30000000;

The ScriptTimeout property is explicitly and automatically set in the constructor of the dynamically created class that represents the page.

Methods of the HttpServerUtility Class


Table 16-8 lists all methods exposed by the HttpServerUtility class. As you can see, they constitute a group of helper methods that come in handy at various stages of page execution. The class provides a couple of methods to create instances of COM components

Return Main Page Previous Page Next Page

®Online Book Reader