HttpHandler redirection
I want to write an HttpHandler to redirect traffic to various web pages on the server. The user will enter http://www.thisissupposedtoberedirected.com/site12
and should be redirected to the appropriate site, in this example site version 1.2
I know how to program in ASP.NET and C #, but I don't seem to understand the finer granularity of site management.
How can i do this? What to do in web.config? I've read this msdn page but it doesn't help much.
a source to share
HttpHandlers are actually pretty simple components.
First you need to create a class that inherits either IHttpHandler
or IHttpAsyncHandler
(for your use, I suggest IHttpHandler
as it doesn't really do the heavy lifting).
Then you compile the DLL and move it into the bin folder of your web application.
Now for the tricky part. Deploying HttpHandlers in the web.config file is tricky because it differs from IIS6, IIS7 Integrated Mode, and IIS7 Classic Mode. The best place to look is this MSDN page:
IIS6
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly" />
</httpHandlers>
<system.web>
</configuration>
IIS7 Classic Mode
<configuration>
<system.web>
<httpHandlers>
<add verb="*" path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly" />
</httpHandlers>
<system.web>
<system.webServer>
<add name=SampleHandler" verb="*" path="SampleHandler.new"
Modules="IsapiModule"
scriptProcessor="FrameworkPath\aspnet_isapi.dll"
resourceType="File" />
</system.webServer>
</configuration>
Integrated mode IIS7
<configuration>
<system.webServer>
<handlers>
<add name="SampleHandler" verb="*"
path="SampleHandler.new"
type="SampleHandler, SampleHandlerAssembly"
resourceType="Unspecified" />
</handlers>
<system.webServer>
</configuration>
As you can see, each IIS configuration requires entries in several different sections of the web.config file. My suggestion was to add entries to every location so that IIS configuration changes would not break your HttpHandler.
a source to share
1) You need to create a new class that implements IHttpHandler or IHttpAsyncHandler (the latter is when you are very comfortable with managing your threads). Create your logic there.
2) Then modify the web.config file so that you register the handler:
<system.web>
<httpHandlers>
<add verb="*" path="*.htm" type="System.Web.StaticFileHandler"/>
<add verb="*" path="*.html" type="System.Web.StaticFileHandler"/>
<add verb="*" path="*.ico" type="System.Web.StaticFileHandler"/>
</httpHandlers>
</system.web>
This is an example setting in my web.config - yours might be slightly different.
Your HttpHandler should now be registered and based on the data provided in your registration in the web.config, when you request specific URLs you will be mapped to the handler you created instead of the normal ASP.NET page handler.
Also, for your specific problem, I would not recommend writing HttpHandler - I would just redirect DNS or do some lookup in your OnInit code to check the host url and if it is listed in your DB, you redirect yourself based on configuration data.
a source to share