I want to do UrlRewriting. in order to do that I have written a HttpModule that runs upon the AuthorizeRequest
event in the ASP.NET Http Modules chain (before the request is processed by the HttpHandler).
I wrote an abstract class as a basic rewriter which implements the IHttpModule
interface:
public abstract class BaseModuleRewriter : IHttpModule {
public virtual void Init(HttpApplication app) {
// WARNING! This does not work with Windows authentication!
app.AuthorizeRequest += new ventHandler(this.BaseModuleRewriter_AuthorizeRequest);
}
public virtual void Dispose() { }
protected virtual void BaseModuleRewriter_AuthorizeRequest(object sender, EventArgs e) {
HttpApplication app = (HttpApplication)sender;
this.Rewrite(app.Request.Path, app);
}
protected abstract void Rewrite(string requestedPath, HttpApplication app);
}
The real implementation of the module is the following class:
public class ModuleRewriter : BaseModuleRewriter {
protected override void Rewrite(string requestedPath, System.Web.HttpApplication app) {
// Just dummy, if the module works, I should never get to any page other than homepage
UrlRewritingUtils.RewriteUrl(app.Context, "Default.aspx");
}
}
In my web.config I have the following:
<configuration>
...
<system.web>
<httpModules>
<add type="ModuleRewriter" name="ModuleRewriter"/>
</httpModules>
</system.web>
...
</configuration>
Actually this is a simple implementation of Url-Rewriting from an article posted in MSDN Megazine. I just made it simpler, but the approach is that one.
It does not work! When I deploy the module like I told you and request a page, I can get to all my pages of my website, when, on the contrary, I should always be "redirected" to Default.aspx
.
What to do?
First of all, one important information: My application pool is Integrated Mode targeting framework 4.0.
My web site is not compiled. I mean that I do not precompile my web site but I put all my code in the App_Code
directory and let ASP.NET compile it when pages are requested. A direct consequence is that all my assemblies are put in the %sysroot%\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\...
directory.
I read some stuff where modules are deployed in the web.config by specifying the assemply as well, something like:
<configuration>
...
<system.web>
<httpModules>
<add type="ModuleRewriter, ModuleRewriterAssembly" name="ModuleRewriter"/>
</httpModules>
</system.web>
...
</configuration>
But, being my stuff not precompiled, but entirely managed by Asp.Net, I cannot specify a different assembly. Is it needed to solve this problem? If so, what to do?
Thankyou
Depending on your appPool configuration (integrated vs classic mode), try adding
<system.webServer>
<modules>
<add type="ModuleRewriter" name="ModuleRewriter"/>
</modules>
</system.webServer>