HttpHandler Redirect - c#

I want to write an HttpHandler to redirect traffic to various webpages on the server.
The user will type in http://www.thisissupposedtoberedirected.com/site12 and should be redirected to the appropiate site, in this example site version 1.2
I know how to program in ASP.NET and C# but I don't seem to grab the finer detail about website management.
How can I manage to get this done? What should I do in the web.config? I've read this msdn page but it isn't much help.

HttpHandlers are actually fairly simple components.
First, you need to create a class that inherits either IHttpHandler or IHttpAsyncHandler (for your use, I'd suggest IHttpHandler since there's really no heavy lifting being done).
You then compile the DLL and drop it in the bin folder of your web application.
Now the tricky part. Deploying HttpHandlers in the web.config file is tricky since it's different between IIS6, IIS7 Integrated Mode, and IIS7 Classic Mode. The best place to look is this MSDN page:
How to: Register HTTP Handlers
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>
IIS7 Integrated Mode
<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 slightly different sections of the web.config file. My suggestion would be to add entries in each location so that IIS configuration changes don't break your HttpHandler.

1) You need to create a new class that implements IHttpHandler or IHttpAsyncHandler (the latter being when you are very comfortable with managing your own threads). Create your logic there.
2) Then, modify the web.config so that you register your 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 a sample setup in my web.config - yours may vary slightly.
Now, your HttpHandler should be registered and based on the details provided in your registration in the web.config, when you request certain URLs, you will be mapped to the Handler you created instead of the normal ASP.NET Page Handler.
Additionally, for your specific issue, I wouldn't recommend writing a HttpHandler - I would just do a DNS redirect, or do some lookup in your OnInit code to check the Host Url, and if it is one specified in your DB, you do the redirect yourself based on the configuration data.

Related

ASP.net handling all requests for a custom file extension in one ashx file

How can I configure my Asp.net project to handle all requests for a custom file extension like ".coolpix" regardless of the request path in one ashx file.
If you want an application wild solution, a way to you archieve it, is by adding a handler mapping into your web.config:
<system.webServer>
<handlers>
<add name="myExtensionHandler" preCondition="integratedMode" verb="GET" path="*.Extension" type="NameSpace.MyExtentionHandlerClass, DLLAssemblyName" />
</handlers>
</system.webServer>
Your MyExtentionHandlerClass should implement IHttpAsyncHandler or IHttpHandler

In ASP.NET, how to make the aspx file accessible while the httphandler works

I must admit that the demand is weird. I have an asp.net web project. In it, there is a web.config with a handlers node, as below:
<system.webServer>
<handlers>
<add path="*" name="CServiceStack.Factory" type="MyOB.Mega" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
The path is '*' that means all the requests will be handled by the 'MyOB.Mega' module.
I want the aspx files can be visited normally, how to edit the web.config?

issue with Telerik_Web_UI_WebResource_axd

When I run my website, I get the following error message
Cannot add duplicate collection entry of type 'add' with unique key attribute 'name' set to 'Telerik_Web_UI_WebResource_axd'
Here are the contents of web.config...
<httpHandlers>
<add path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource" verb="*" validate="false"/>
</httpHandlers>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true"/>
<validation validateIntegratedModeConfiguration="false"/>
<handlers>
<add name="Telerik_Web_UI_WebResource_axd" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource"/>
</handlers>
</system.webServer>
If I comment out the handlers entry.. I get the following error message...
'~/Telerik.Web.UI.WebResource.axd' is missing in web.config. RadScriptManager requires a HttpHandler registration in web.config. Please, use the control Smart Tag to add the handler automatically, or see the help for more information: Controls > RadScriptManager
I am total at loss on how to fix this. I do not know how to use Smart Tag. I have googled, looked into SO, looked into Telerik site and can not find solution any where. I do not know if the problem is in my web.config, Virtual directory or where??? My colleagues have the same code base and web.config and it works for them.
EDIT
Here is my development machine setup...
Windows 7 Enterprise Service Pack 1 64 bit OS
Visual Studio 2010 Enterprise Service pack 1 IIS version 7.5
Please help.
The problem wont be in the IIS config; as the error message specifically refers to a Web.config file, so that's going to be in .Net
What happens if you comment out the httpHandlers entry rather then handlers ?
Edit: After looking at your web.config file, the only thing I can suggest is changing the format of the system.webServer.handlers.add part from
<add name="Telerik_Web_UI_WebResource_axd" verb="*" preCondition="integratedMode" path="Telerik.Web.UI.WebResource.axd" type="Telerik.Web.UI.WebResource"/>
to:
<add name="Telerik.Web.UI.WebResource" path="Telerik.Web.UI.WebResource.axd" verb="*" type="Telerik.Web.UI.WebResource, Telerik.Web.UI" />
And that's because that's what Telerik suggest to be the best practice; If that doesn't work then I'm sorry but I'm out of ideas tonight, but please leave the question open, perhaps someone with a better idea will see it in the morning.

REST with .NET using a Default Handler

I am creating a REST service using .NET and have used a generic handler, Default.ashx, to handle the incoming request. This allows me to access the service using methods like "http://rest/test" without issue. But when a file extension is added IIS no longer redirects the request but instead looks for a file. How can the web.config be modified so that URL request like "http://foo/test.xml" and "http://foo/test.json" are also handled by the DefaultHandler.ashx? I have done this before so know its possible but cannot remember the configuration.
You can see an example of this in my HttpClone app's web.config. The gist of it involves removing handlers for the extensions you don't want like this:
<system.webServer>
...
<handlers accessPolicy="Read, Script">
<clear />
<add name="Favorite-Icon" path="/favicon.ico" verb="GET,HEAD" modules="StaticFileModule" resourceType="File" requireAccess="Read" />
<add name="HttpClone" path="*" verb="GET,HEAD,POST,DEBUG" type="Namespace.MyCustomHandler, AssemblyName" resourceType="Unspecified" requireAccess="Script" preCondition="integratedMode" />
</handlers>
This says to use a custom handler for all requests for all URIs except '/favicon.ico'. If you still need to use the 'Default.ashx' then you will need to get handler for the ashx type and map it to handle all paths just like this. Generally though there is no need for the ashx extension handler, just implement IHttpHandler in any assembly and reference it in the 'type' attribute above.
Note that the cassini web server (the test server in VStudio) will not map the default directory '/' to you're handler. To fix this for cassini you need a default.aspx document to exist (although it can be empty).
Note 2 - The configuration above is for integrated mode only, for classic mode the concept is the same but the settings are in a different location.

Disable Razors default .cshtml handler in a ASP.NET Web Application

Does anyone know how to disable the .cshtml extension completely from an ASP.NET Web Application?
In essence I want to hijack the .cshtml extension and provide my own implementation based on a RazorEngine host, although when I try to access the page.cshtml directly it appears to be running under an existing WebPages razor host that I'm trying to disable.
Note: it looks like its executing
.cshtml pages under the
System.Web.WebPages.Razor context
as the Microsoft.Data
Database is initialized. I don't even
have any Mvc or WebPages dlls
referenced, just System.Web.dll and a
local copy of System.Web.Razor with
RazorEngine.dll
I've created a new ASP.NET Web .NET 4.0 Application and have tried to clear all buildProviders and handlers as seen below:
<system.web>
<httpModules>
<clear/>
</httpModules>
<compilation debug="true" targetFramework="4.0">
<buildProviders>
<clear/>
</buildProviders>
</compilation>
<httpHandlers>
<clear/>
<add path="*" type="MyHandler" verb="*"/>
</httpHandlers>
</system.web>
<system.webServer>
<modules runAllManagedModulesForAllRequests="true">
<clear/>
</modules>
<handlers>
<clear/>
<add path="*" name="MyHandler" type="MyHandler" verb="*" preCondition="integratedMode" resourceType="Unspecified" allowPathInfo="true" />
</handlers>
</system.webServer>
Although even with this, when I visit any page.cshtml page it still bypasses My wildcard handler and tries to execute the page itself.
Basically I want to remove all traces of .cshtml handlers/buildProviders/preprocessing so I can serve the .cshtml pages myself, anyone know how I can do this?
If you're trying to turn off ASP.NET webpages, you can set this flag in app settings:
<add key="webpages:Enabled" value="false" />
You should be able to register your own custom ViewEngine in the Application_Start method. Scott Hanselman blogged a sample that uses a custom ViewEngine for mobile devices, but the ideas should be the same for what you're trying to do.
Edit (again): David Fowler suggests:
<add key="webpages:Enabled" value="false" />
I always wondered what that setting was for, but never got around to investigating! :-)

Categories

Resources