Correct way to use Response Redirect in .NET

  • Thread starter Thread starter Daniel
  • Start date Start date
D

Daniel

Hello, I have an old asp page that was used to redirect users called
redirect.asp:

<code1>
<%
Option Explicit

If Request.QueryString("url") <> "" Then
Response.Redirect Request.QueryString("url")
Else
Response.Redirect ("www.examplesite.asp")
End If
%>
</code1>


So far I have this:

<code2>
<%@ Page Language="vb" enableviewstate="False"%>
<script runat=server>
Public Sub Page_Load()
Response.Redirect ("http://www.tv.dlpsaleslab.com/default.aspx")
End Sub
</script>
</code2>

My question is how do I use VB.NET to do this.
When I add the above code I am given an error, since I am new to VB,
can someone help advise what is the proper way to set up a
Response.Redirect in the manner as I have done in in code1 using
VB.NET?

Thank you,
 
The need for Response.Redirect is greatly diminished in .NET. Look into
Page.IsPostBack.
 
So to clarify, how to I use Page.IsPostBack in this manner to accomplish
the same as I have done in the first bit of code?

Any help would be appreciated?

Thanks,
 
Here is a brief explanation of how and when to use...

Response.Redirect functions in ASP.NET exactly as it did in ASP
as the intrinsic ASP objects have been carried over to ASP.NET
for backward compatibility.

Using Response.Redirect requires two trips to the server and imposes
performance penalties and should only be used when the three
following circumstances demand its use...

1.) When redirecting to a remote server.
2.) When redirecting to a different website on the same server.
3.) When redirecting to static pages, i.e. from .asp to .htm, .aspx to .htm
or to pages generated by different application servers such as .asp to .php
and .aspx to .php.

Otherwise use the Server.Transfer method or Server.Execute as the
circumstances may require.
 
It looks like you were trying to see if the page had ever been loaded before
(if it had, there would be a querystring value). So, in .NET we'd do:

If IsPostBack Then
Response.Redirect Request.QueryString("url")
Else
Response.Redirect ("www.examplesite.asp")
End If

Using Redirect only in circumstances previously posted.
 
So since i am doing both 2,3 here using Response.Redirect is the right
way to go.

So in fact this seems to work, but as far as using ASP.NET correctly is
this the proper way to format it, if so thank you very much.

<%@ Page Language="vb" enableviewstate="False"%>
<script runat="server">
Public Sub Page_Load()

If Request.QueryString("url") <> "" Then
Response.Redirect Request.QueryString("url")
Else
Response.Redirect ("http://www.example/default.aspx")
End If

End Sub
</script>
 
Thanks for the correction, however when I link to this file from another
page, I get an error:

Compilation Error
Description: An error occurred during the compilation of a resource
required to service this request. Please review the following specific
error details and modify your source code appropriately.

Compiler Error Message: BC30800: Method arguments must be enclosed in
parentheses.

Source Error:


Line 4:
Line 5: If IsPostBack Then
Line 6: Response.Redirect Request.QueryString("url")
Line 7: Else
Line 8: Response.Redirect
("http://www.example.com/default.aspx")
 
Thanks for your help the following code seems to achive what I am
looking for.

<code>

<%@ Page Language="vb" enableviewstate="False"%>
<script runat="server">
Public Sub Page_Load()

If IsPostBack Then
Response.Redirect (Request.QueryString("url"))
Else
Response.Redirect ("http://www.example.com/default.aspx")
End If

End Sub
</script>

</code>
 
It just means that in .NET, you must pass method arguments in parenthesis.
Your first redirect doesn't have any parenthesis. Try this:


If IsPostBack Then
Response.Redirect(Request.QueryString("url"))
Else
Response.Redirect("http://www.example.com/default.aspx")
End If
 
Back
Top