
With classic ASP, the only way to utilize URL rewriting was to write an ISAPI filter or to buy a third-party product that offered URL rewriting capabilities. With Microsoft® ASP.NET, however, you can easily create your own URL rewriting software in a number of ways.
What Happens When a Request Reaches IIS
if a request comes in for a Web page named Info.asp, IIS will route the message to the asp.dll ISAPI extension. This ISAPI extension will then load the requested ASP page, execute it, and return its rendered HTML to IIS, which will then send it back to the requesting client. For ASP.NET pages, IIS routes the message to the aspnet_isapi.dll ISAPI extension. The aspnet_isapi.dll ISAPI extension then hands off processing to the managed ASP.NET worker process, which processes the request, returning the ASP.NET Web page's rendered HTML.
“You can customize IIS to specify what extensions are mapped to what ISAPI extensions. the ASP.NET-related extensions—.aspx, .ascx, .config, .asmx, .rem, .cs, .vb, and others—are all mapped to the aspnet_isapi.dll ISAPI extension.”
ISAPI filters have numerous applications
including:
- Authentication and authorization.
- Logging and monitoring.
- HTTP compression.
- URL rewriting.
ASP.NET includes a number of built-in HTTP handlers
The PageHandlerFactory, for example, is used to render ASP.NET Web pages. The WebServiceHandlerFactory is used to render the response SOAP envelopes for ASP.NET Web services. The TraceHandler renders the HTML markup for requests to trace.axd.
how a request for an ASP.NET resource is handled. First, IIS receives the request and dispatches it to aspnet_isapi.dll. Next, the ASP.NET engine initializes the configured HTTP modules. Finally, the proper HTTP handler is invoked and the requested resource is rendered, returning the generated markup back to IIS and back to the requesting client.
Creating and Registering Custom HTTP Modules and HTTP Handlers
Creating custom HTTP modules and HTTP handlers are relatively simple tasks, which involve created a managed class that implements the correct interface. HTTP modules must implement the System.Web.IHttpModule interface, while HTTP handlers and HTTP handler factories must implement the System.Web.IHttpHandler interface andSystem.Web.IHttpHandlerFactory interface, respectively.
Once a custom HTTP module or HTTP handler has been created, it must be registered with the Web application. Registering HTTP modules and HTTP handlers for an entire Web server requires only a simple addition to the machine.config file; registering an HTTP module or HTTP handler for a specific Web application involves adding a few lines of XML to the application's Web.config file.
Specifically, to add an HTTP module to a Web application, add the following lines in the Web.config's configuration/system.web section:
<httpModules>
<add type="type" name="name" />
</httpModules>
The type value provides the assembly and class name of the HTTP module, whereas the name value provides a friendly name by which the HTTP module can be referred to in the Global.asax file.
HTTP handlers and HTTP handler factories are configured by the <httpHandlers> tag in the Web.config's configuration/system.web section, like so:
<httpHandlers>
<add verb="verb" path="path" type="type" />
</httpHandlers>
Recall that for each incoming request, the ASP.NET engine determines what HTTP handler should be used to render the request. This decision is made based on the incoming requests verb and path. The verb specifies what type of HTTP request was made—GET or POST—whereas the path specifies the location and filename of the file requested. So, if we wanted to have an HTTP handler handle all requests—either GET or POST—for files with the .scott extension, we'd add the following to the Web.config file:
Implementing URL Rewriting
- URL Rewriting with HTTP Modules
- URL Rewriting in HTTP Handlers
- Performing Simple URL Rewriting with the URL Rewriting Engine
<RewriterConfig>
<Rules>
<!-- Rules for Product Lister -->
<RewriterRule>
<LookFor>~/Products/Beverages\.aspx</LookFor>
<SendTo>~/ListProductsByCategory.aspx?CategoryID=1
</SendTo>
</RewriterRule>
<RewriterRule>
</Rules>
</RewriterConfig>
<httpHandlers>
<add verb="*" path="*.scott" type="type" />
</httpHandlers>
<url name="user_ForgotPassword"
path="EmailForgottenPassword.aspx"
pattern="emailforgottenpassword.aspx"
physicalPath="##themeDir##"
vanity="{2}" page="forgottenpassword.aspx" />
Comments