Computer Programming Tutorials Navigation

 
Web ask-4it.com
look-4it.com try-2-find.com

Dynamic Redirect From ASP To ASP.NET Web Page Based On Page Parameter


by Tymofiy Popov

Who should read this article?

This article is intended for web programmers who work under mixed ASP/ASP.NET projects or need to port old ASP projects to new ASP.NET platform.

How dynamic redirect works?

One of the practical questions I was asked for: "How to redirect dynamic URLs to their new counterparts if you port your site from ASP to ASP.NET? You cannot use internal IIS features because of hosting limitations."

First of all, how to redirect user's browser from one page to another on the server side? You should send special HTTP header to the user with information about new URL. On ASP we can do it using code below:

Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www.yourwebsite.com"
Response.End

Note please, that this code should be executed before any headers or content sent to the user. The command Response.End finalizes ASP processing, so redirecting will be the last action which your ASP page do.

Handling page parameters

Now we know how to redirect user to another URL. How to dial with page parameters?

We should read them on our ASP page and attach to new target URL. For example, you have old productview.asp page which receives "productid" parameter and you want to redirect it to new details.aspx page with "id" parameter. Code below demonstrates how to implement that:

<%@ Language = VBScript %>
<%
' Read the parameter
Dim ProductID
ProductID = Request.QueryString("productid")

' Check the ID
If Not IsNumeric(ProductID) Then
   ProductID = "111" ' Set product ID to some default value
End If
' Check the ID

' Redirect customer to the new URL
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www. yourwebsite.com/detail.aspx?id=" & ProductID
Response.End
%>

We should insert this code to our old productview.asp ASP page and all customers who hit this page (from the search results for example) will be correctly redirected to the new ASP.NET site version.

We can use multiple parameters as well. For example you have an old productlist.asp ASP page which receives "categoryid" and "brand" parameters and you want to redirect it to new searchresults.aspx ASP.NET page with "catid" and "brand" parameters.

<%@ Language = VBScript %>
<%
' Read the parameters
Dim CategoryID
CategoryID = Request.QueryString("categoryid")

Dim BrandName
BrandName = Request.QueryString("brand")

' Check the ID
If Not IsNumeric(CategoryID) Then
   CategoryID = "111" ' Set category ID to some default category
End If
' Check the ID

' Redirect customer to the new URL
Response.Status = "301 Moved Permanently"
Response.AddHeader "Location", "http://www.yourwebsite.com/searchresults.aspx?catid=" & _
CategoryID & "&brand=" & Server.URLEncode(BrandName)

Response.End
%>

Note please, that we use URLEncode method of the Server object for BrandName parameter encoding. Because brand name is a character value, spaces and other characters can be found there. So we should prepare this data for sending as the URL parameter.




Back to article category: ASP Programming

Related Information



Related Searches



 
Web ask-4it.com
look-4it.com try-2-find.com





Home  |  Tools  |  Downloads  |  RSS Feed  |  Link to Us  |  Contact Us  |  Privacy Policy  |  Terms of Use

Copyright © 2005, ASK-4IT.COM. All Rights Reserved.

All trademarks, icons, and logos, shown or mentioned at this web site, are the property of their respective owners.