views:

529

answers:

4

We are replacing an old classic asp website with a .NET 3.5 solution.

We need to redirect all of the classic ASP requests to aspx pages (i.e. contactus.asp, may now route to /contact-us/default.aspx). What I woudl like is for the requests to hit global.asax so I can do something like

If url == "bob.asp"
    Response.Status = "301 Moved Permanently";
    Response.AddHeader("Location", SiteConfig.SiteURL + redirectUrl);
End If

There are two inelegant solutions.

A) Place a global.asa file and do the routing through that.

B) Map asp files to the .NET engine. Great, but then if we need to host classic asp sites on our sites IIS will be sending the requests to the wrong place.

I found a nice solution here

http://forums.asp.net/p/1202225/3458901.aspx

Which stated something like this may work...

<buildProviders>

<add extension=".php" type="System.Web.Compilation.PageBuildProvider" />

</buildProviders>
<httpHandlers>

<add verb="*" path="*.php" type="System.Web.UI.PageHandlerFactory" validate="True" />

</httpHandlers>

This example was for php but I assume the same thing would work for asp. However after changing .php to .asp in the example and placing the tags in the correct part of the web.config I'm having no joy (a 500 server error actually).

Can anyone shed any light on this or give me an elegant solution.

Had a feeling the above solution wouldnt work for php or asp as IIS will have routed the request before it gets to the .NET engine.

Thanks in advance

Steve

+1  A: 

The simplest thing is to use a custom 404 error page at the IIS level. This page can be any ASPX page; you have access to the original request via the HttpContext.

In IIS 6.0, you'll notice that by default URLs mapped to the .NET engine (.aspx,,asmx etc) use their own 404 handler defined in web.config. No matter; you can cause even those pages to be sent to the IIS 404 page if you edit the ASPX engine mapping and ensure that the tickbox for "file must exist" is set. This redirects all boken links to the IIS 404 handler, even if they are mapped to .NET handlers. People make the mistake of trying to redirect all broken links to the web.config defined 404handler; it's must easier if you just force all to go to the IIS defined one instread.

x0n
We've tended to go down this route as well - although we generally set the 404 handler in both IIS and in the web.config to point to the same aspx page. That page then talks to a database of mappings (old/vanity/etc urls to new urls).
Zhaph - Ben Duguid
+2  A: 
Eduardo Molteni
Thanks for the detailed reply. Shall try to do it this way - thanks for all the responses people!
CountZero
A: 

I use a modified version of the Smart 404 Handler script from http://evolvedcode.net/content/code_smart404/. I added code to do custom mapping based of a table in our database.

This script could easily be re-written in ASP.NET and then mapped the same way.

Mike J
A: 

Eduardo Molteni's answer works, except for one thing. It actually passes the browser a 302 instead of a 301.

I believe instead of:

Context.Response.StatusCode = 301 
Context.Response.Redirect("/something/") 

it should be:

Context.Response.StatusCode = 301 
Context.Response.RedirectLocation = "/something") 

The response.redirect is basically interrupting what you were setting up with the Response.StatusCode and the browser ends up getting a "302 Found".

I'm not really sure how search engines handle a 302 vs a 301 so maybe its not an issue. It seems to me though that a true Permanent Redirect (301) would be preferred.

For those on v4 of the framework, there seems to be a new option:

Response.RedirectPermanent("/something")

I haven't tested this, but I'm assuming it provides a 301 as the status code. Details here: HttpResponse.RedirectPermanent

Jeremy Mattingly