Redirecting in ASP.NET - asp.net

I recently deployed a new version of an ASP.NET website. This site is built from scratch so urls that worked on the old site do not work on the new one. I've discovered that some of the old pages are requested quite a lot, so I'd like to redirect users trying to get those pages to corresponding new ones.
Let's say the old site had this page:
www.mysite.com/mypage.aspx
And the new site has this corresponding page:
www.mysite.com/pages/mypage.aspx
What is a good (the best) way to redirect requests for the old page to the new page?
My first though was using url mappings in web.config, but that doesn't work for me. The reason is that no real redirect occurs so the right page is shown but the url is not updated in the browser, which leads to the problem that all my relative links get messed up.
Of course I could just recreate the old ASPX pages on the new site and redirect from those, but I don't want to have this "garbage files" in my web project.
Preferably I'd like a solution where I could do all this redirecting in one place. That's what I liked about using url mappings in web.config, but that solution had other problems as mentioned.
Any suggestions?

There are 2 things that come to my mind right now.
A find & replace which is cumbersome yet effective. This is probably
or
You can use the html base tag
http://www.w3schools.com/tags/tag_base.asp
http://www.htmlcodetutorial.com/linking/_BASE.html
But keep in mind that this actually changes ALL the url's (including img urls) in a page.

If it's just a few pages, you could register one single handler for each of the urls and then in the handler redirect to the correct page.
For example
<add name="GetMemberPortrait.aspx_*" path="GetMemberPortrait.aspx" verb="*" type="ExposureRoom.Web.GetMemberPortrait" preCondition="integratedMode"/>
In this web.config file entry (in the handlers section) you see that for the path"GetMemberPortrait.aspx" a specific handler has been registered. That is for that specific "aspx" page the hander specified is instantiated.
The handler can just be a .cs file that implements the IHttpHandler interface. And you can use the same handler to handle all such cases.
For each url, you'll need an entry but you can use the same handler. so for example if you have 3 urls that need redirection called
1. page1.aspx
2. page2.aspx
3. page3.aspx
<add name="page1.aspx_*" path="page1.aspx" verb="*" type="SomeNameSpace.RedirectorHandler" preCondition="integratedMode"/>
<add name="page2.aspx_*" path="page2.aspx" verb="*" type="SomeNameSpace.RedirectorHandler" preCondition="integratedMode"/>
<add name="page3.aspx_*" path="page3.aspx" verb="*" type="SomeNameSpace.RedirectorHandler" preCondition="integratedMode"/>
In your handler, you'll get all of the request parameters as well so you can use those to forward on to the redirected page.

Using the URL Rewrite module for IIS, see http://learn.iis.net/page.aspx/460/using-the-url-rewrite-module/

Related

Routing an ASP page to a Action in ASP.NET MVC

I am recreating a site from VB .NET to MVC 4 ASP.NET. In the old project there is a system that sends an email to a user and within that email there is a hyperlink that can direct the user to view the specific task. I want to make all of the old emails work with the new recreated site. Here is an example of the hyperlink in the old system:
http://pppweb/accounting/ap/default.asp?etaskid=32698
And here is an example of what a hyperlink looks on the new system.
http://pppweb/accounting/ap/ApIssues/Task/32698
So I was thinking i could create a default.asp page in my MVC project and try and have it parse the taskID out. I am not exactly sure the best way to go about doing this?
I would advise against adding a classic .asp (not .aspx?) page in your project, since I'm not even sure there is a way to do that. You can, assuming you are running IIS7, map the url:
http://pppweb/accounting/ap/default.asp
to be handled by MVC by adding the following line to your web.config file:
<configuration>
...
<system.webServer>
...
<handlers>
...
<add name="aspnet htm" path="account/api/default.asp" verb="*" type="System.Web.Mvc.MvcHttpHandler" preCondition="integratedMode" />
Then, add a route for that url and requests should resolve to the normal route parameters you specify. From that point, you should be able to access etaskid from the query string easily enough.
Edit:
Alternatively, if the url matters a lot to you, you can specify a redirect rule from the old format url to the new one by using UrlRewrite module (also an IIS 7 feature). You can read about it here: http://www.iis.net/downloads/microsoft/url-rewrite

Redirect ASP pages to a new URL

Our old website was built using ASP and now we are using Wordpress (IIS) utilizing a sub-directory. All of the old ASP files are still in the root directory and I'd like to redirect all of these file extensions to our new Wordpress site because the old ASP files are causing 404 errors.
I'm having a real difficult time figuring out what to put in my web.config file so all the old ASP pages are redirected to our new home page.
Thanks
The best option is to use the URL Rewriting extension for IIS ( http://www.iis.net/downloads/microsoft/url-rewrite ) which can also perform regex-based redirections. Create a new rule that matches (.+)\.asp and redirects as appropriate.
I don't know if you just want to do a 'dumb' redirect from all .asp-URIs to your WordPress root, or if you want to intelligently redirect users from the old .asp pages to their new WordPress equivalents (this is a good idea as it means search engine results and previous links won't be broken, I'm sure you can imagine how frustrating it is to follow a link that doesn't go where the user expects).
Thanks everyone for your suggestions, but because I don't have access to the server, my only solution was to work with the web.config file. I simply added the following code and everything now works:
<directoryBrowse enabled="true" />
<httpRedirect enabled="true" exactDestination="true" httpResponseStatus="Found">
<add wildcard="*.asp" destination="/" />
</httpRedirect>

ASP.NET redirect rule for .cfm file

I have a DotNetNuke application where I am migrating all my old web sites all together under portals.
As users are storing old sites URLs into bookmarks, I need to write redirect rules so they can get properly on new page.
Now, at one point I stuck because I am unable to write rule.
My condition is
URL A
http://www.mydomain.com.cn/auto.cfm?myurl=domain/notice_article.cfm
Should redirect to
http://www.mynewdomain.com/home.aspx
I have implemented this using redirectMaps.
e.g.
<rewriteMap name="MyRedirectMap_Entire">
<add key="whttp://www.mydomain.com.cn/auto.cfm?myurl=domain/notice_article.cfm" value="http://www.mynewdomain.com/home.aspx" />
</rewriteMap>
Adding this properly under web.config file, I able to redirect properly.

Best way to handle old *.asp links with Asp.Net 4.0 on IIS 7.5?

We still have a lot of incoming urls pointing to our old .asp pages. The new .aspx pages are called the same as the old .asp pages, and accept mostly the same values, so they are pretty much interchangeable. Unfortunately we still get both POST and GET requests to the old .asp pages.
We handled these .asp requests in our Asp.Net 2.0 app by adding this in our web.config (system.webserver section):
<handlers>
<add name="ASPClassicForRedirectToASPX"
path="*.asp"
verb="*"
modules="IsapiModule"
scriptProcessor="%windir%\Microsoft.NET\Framework\v4.0.30319\aspnet_isapi.dll"
resourceType="Unspecified"
preCondition="classicMode,runtimeVersionv4.0,bitness32" />
</handlers>
and then checking in BeginRequest if the url ends with .asp, and if it does, do a RewritePath to .aspx
Now we have migrated to asp.net 4.0 and I started to wonder if there is a better way to handle this, and still keep backwards compatibility with the old .asp urls?
what i would do if not allready the case is something like that in the BeginRequest instead of url rewrite (when possible) :
Response.Clear();
Response.Status = "301 Moved Permanently";
Response.AddHeader("Location","New-url");
Response.End();
using a 301 permanent redirect you will make sure that all crawlers and stuff will change their link to the right one that should reduse the calls u get to your .asp pages by a lot. other than that you are stuck with the old direct links to your asp pages at some point im guessing you will have to let them go. Cant support old stuff forever even tho that would be awesome :)

Extensionless Page Requests

I have a customer of ours that sent out a publication referring to my site but they used the wrong address...they did not provide a page name...it looks like this:
mywebsite.org/Resources/toolkits/bridging
when it should have been
mywebsite.org/Resources/toolkits/bridging/default.aspx
Is there a way to tell ASP.NET to default to this default.aspx when it sees this kind of request or even better, have IIS 7 handle this easily?
This site is live so I would like to avoid having to introduce code if possible.
As per other suggestions, this should be done in the IIS configuration for your website using the IIS Admin tool.
There is however, another alternative - you can add a section in the web.config of your actual ASP.NET application, allowing you to override the IIS configuration right from your application:
<system.webServer>
<defaultDocument>
<files>
<clear />
<!-- Specify each of your files by order of preference here -->
<add value="Default.aspx" />
<add value="Index.aspx" />
<add value="MyOtherPage.aspx" />
</files>
</defaultDocument>
</system.webServer>
The caveat to this though is it may be a little obtuse when the IIS administrator can't figure out why the server configuration isn't working the way he's got it configured. It's not always right to do something just because you can.
Finally, just in case you don't have access to the IIS server or your IIS administrator has reasons for not adding Default.aspx to the default document list in the IIS configuration and for whatever reason, you don't wish to override the IIS configuration in your web.config file, then the quickest and simplest way is to simply create a file called default.asp in that directory containing:
<% Response.Redirect("default.aspx") %>
Default.asp is in the default document list on IIS. The code will automatically redirect the call to the correct page. The downside to this approach though is that there's a performance hit - every time someone calls default.asp - directly or otherwise, the redirect needs to happen which isn't free.
In the Documents tab of the web site properties in IIS you can specify default documents. If you are using .Net2.0 or later on that machine then Default.aspx should already be set....
Default.aspx is not, oddly enough, set as the default document in an IIS installation; In IIS 7, the setting is under "HTTP Features", called "Default Document". Add default.aspx to that list and you should be OK.
If not, you'll need to add a 404 handler that redirects when it sees that URL.

Resources