Asked by:
Simpler Response.Redirect

Question
-
User-668204308 posted
Hey all. Here's some code I developed since I've grown tired of wasting time by typing down the page's name when I want to go to the same page and inserting weird code like "?id=" just to get sent to a new page with querystring attached. I thought I could share it since it's become really useful to me by saving lots of typing time.
This allows you to redirect yourself to a new page with or without QueryString (by passing the value down in the second field of the sub) or to the same page you're in by typing "myself" on the first field of the sub.This only allows ONE querystring value.
-------------------------------
Public Class Travel
Shared Sub Into(ByVal destiny As String, Optional ByVal valuetotransport As String = "")
If Trim(LCase(destiny)) <> "myself" And Trim(LCase(destiny)) <> "" Then
If valuetotransport <> "" Then
HttpContext.Current.Response.Redirect(destiny + "?value=" + valuetotransport)
Else
HttpContext.Current.Server.Transfer(destiny)
End If
ElseIf Trim(LCase(destiny)) = "myself" And Trim(LCase(destiny)) <> "" Then
If valuetotransport <> "" Then
HttpContext.Current.Response.Redirect(GetCurrentPageName() + "?value=" + valuetotransport)
Else
HttpContext.Current.Server.Transfer(GetCurrentPageName())
End If
End If
End Sub
Shared Function GetCurrentPageName() As String
Dim sPath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
Dim oInfo As System.IO.FileInfo = New System.IO.FileInfo(sPath)
Dim sRet As String = oInfo.Name
Return sRet
End Function
End Class
-----------------------------------------
Wednesday, June 1, 2011 8:01 AM
All replies
-
User-693248168 posted
Hi, The methods you have developed are good.
But what if
1. the user doesnt have the query string name as "Value". Maybe the user want some other name.
2. the user wants to send multiple values in the query string.
I think with little more effort you can come up with a better solution.
Wednesday, June 1, 2011 8:49 AM -
User-668204308 posted
The first one is easy to do, but for the second one, I'd need to have specific number of values passed onto the sub, otherwise the only choice is to use an array, and that would take more or the same number of characters as writing a querystring line. Do you really think it's worth doing it?
Wednesday, June 1, 2011 9:09 AM -
User-668204308 posted
Public Class Travel
Shared Sub Into(ByVal destiny As String, Optional ByVal valuetotransport As String = "", Optional ByVal valuename As String = "value")
Dim VN As String = Trim(valuename)
If Trim(LCase(destiny)) <> "myself" And Trim(LCase(destiny)) <> "" Then
If valuetotransport <> "" Then
HttpContext.Current.Response.Redirect(destiny + "?" + VN + "=" + valuetotransport)
Else
HttpContext.Current.Server.Transfer(destiny)
End If
ElseIf Trim(LCase(destiny)) = "myself" And Trim(LCase(destiny)) <> "" Then
If valuetotransport <> "" Then
HttpContext.Current.Response.Redirect(GetCurrentPageName() + "?" + VN + "=" + valuetotransport)
Else
HttpContext.Current.Server.Transfer(GetCurrentPageName())
End If
End If
End Sub
Shared Function GetCurrentPageName() As String
Dim sPath As String = System.Web.HttpContext.Current.Request.Url.AbsolutePath
Dim oInfo As System.IO.FileInfo = New System.IO.FileInfo(sPath)
Dim sRet As String = oInfo.Name
Return sRet
End Function
End Class
------
^ Now with value names
Wednesday, June 1, 2011 9:15 AM -
User-668204308 posted
I thought I should also prevent the user from traveling to other non-existant pages with something not very annoying like an exception so I'm working on it too.
Wednesday, June 1, 2011 9:31 AM -
User-693248168 posted
How are you planning to make sure the page is non-existant?
Wednesday, June 1, 2011 9:45 AM -
User-668204308 posted
Good point. I can't. That would stop them from going to web pages on the internet or their computer. I didn't consider that.
Wednesday, June 1, 2011 9:57 AM