Redirecting in ASP.NET
I recently deployed a new version of an ASP.NET website. This site is built from scratch so u开发者_StackOverflowrls 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/
精彩评论