Safe Response.Redirect?

U

Urs Eichmann

If I have two webforms, form1.aspx and form2.aspx, and I want to do a
Response.Redirect from form1 to form2, I can write

Response.Redirect("form2.aspx")

However, if somebody changes the name of form2, the redirect will no
longer work. I could write

Response.Redirect(GetType(form2).Name + ".aspx")

but this is quite ugly and will still not work i.e. if the form2 is
being moved to a sub folder. Worse, I'll only detect it at runtime and
get no compiler warning or such. Compared to winforms, when I'm writing

Dim f as New Form1
f.ShowDialog

if I now change the name of the Form1 class, or the namespace, I will
get a compiler warning.

Is there any "safe" possiblity of redirecting to another webform which
doesn't rely on the physical name and location of an aspx file, and
could be detected at compile time?

Perhaps I'm missing something obvious here...

Thanks for any help
Urs
 
J

Josh

Moving from Windows to Web leads to a lot of these questions. I dont belive
there is a way of doing what you want without storing the file(page) names
ans paths in a datastore.
 
U

Urs Eichmann

Josh said:
Moving from Windows to Web leads to a lot of these questions. I dont belive
there is a way of doing what you want without storing the file(page) names
ans paths in a datastore.
even if you do that, when somebody renames a page without syncing the
change in the datastore, you're out of luck... :-(
 
H

Hans Kesting

Urs said:
If I have two webforms, form1.aspx and form2.aspx, and I want to do a
Response.Redirect from form1 to form2, I can write

Response.Redirect("form2.aspx")

However, if somebody changes the name of form2, the redirect will no
longer work. I could write

Response.Redirect(GetType(form2).Name + ".aspx")

but this is quite ugly and will still not work i.e. if the form2 is
being moved to a sub folder.

and it might not even work in the "normal" case: File "Default.aspx"
uses a classname of "_Default". The filename doesn't *need* to be the
same as the classname.

Hans Kesting
 
V

vMike

Urs Eichmann said:
If I have two webforms, form1.aspx and form2.aspx, and I want to do a
Response.Redirect from form1 to form2, I can write

Response.Redirect("form2.aspx")

However, if somebody changes the name of form2, the redirect will no
longer work. I could write
If it is something that is going to change you could put a key in your
web.config file and get the key in the redirect. That way it would be easier
to modify.

Mike
 
U

Urs Eichmann

If it is something that is going to change you could put a key in your
web.config file and get the key in the redirect. That way it would be easier
to modify.

Mike,
the problem with this and also the datastore approach of Josh is that
you always have to to changes in *two* locations (change file name and
web.config, or change file name and datastore). This is very
error-prone, compared to the winforms approach I wrote about in my
initial post, where you change the form name *once* and you will be
automatically reminded by the compiler if you forget to change it
anywhere else.

I know this might sound ridiculous to some, but in my code I always
program things in just *one* place so I can never run into sync
problems. It disturbs me that there seems to be no way to do it in this
case.

Urs
 
J

Josh

problems. It disturbs me that there seems to be no way to do it in this

You'll find many issues like this in the Web's Stateless environment.
However you caould always write a checking routine as a Visual Studio
Add-In. There might even be one you can download.
 
S

Steven Cheng[MSFT]

Hi Urs,

Yes, the problem you mentioned is the existing limitation of web page based
application. In fact, web application started from pure page based
techniques such as ASP, PHP which only have pages (contains scripts) in
webserver's virtual directory and there are even no class or codebehind
concepts at that time. The ASP.NET encaspulate the dynamic web page's
processing and provide such as webform framework, make the web page
developing most like desktop form developing. However, the fundamental
mechanism is still page/file/directory based, the vritual path rules is
very critical for such application.

In addition, as for using web.config/datastore to store the page name, we
still need to change two places when the page file changes , this is also
limited by the fact that webform page file is not directly 1:1 associated
with page class. Multi aspx page can associated with the same code behind
class and their name can vary.

Anyway, I still think using web.config to store the key/value pair is a
reasonable approach currently.

Thanks,

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 
U

Urs Eichmann

Steven,
Thanks for you response. Let's say I have form1.aspx and form2.aspx.
From the codebehind of form1.aspx, is there a way to get the aspx file
name and path of form2? I mean something such as

Sub DoRedirectToForm2()
Dim f as new form2
dim path as string = f.GetRelativeFileAndPath()
f.dispose
response.redirect(path)
end sub

GetRelativeFileAndPath would be a function which retrieves the ASPX file
name and path relative to the path of the current page. Is there such a
function, or a way to get this information? With this, we could do a
redirect without depending on hardcoded file names.

Regards,
Urs
 
U

Urs Eichmann

Steven,
Thanks for you response. Let's say I have form1.aspx and form2.aspx.
From the codebehind of form1.aspx, is there a way to get the aspx file
name and path of form2? I mean something such as

Sub DoRedirectToForm2()
Dim f as new form2
dim path as string = f.GetRelativeFileAndPath()
f.dispose
response.redirect(path)
end sub

GetRelativeFileAndPath would be a function which retrieves the ASPX file
name and path relative to the path of the current page. Is there such a
function, or a way to get this information? With this, we could do a
redirect without depending on hardcoded file names.

Regards,
Urs


Hi Urs,

Yes, the problem you mentioned is the existing limitation of web page
based application. In fact, web application started from pure page based
techniques such as ASP, PHP which only have pages (contains scripts) in
webserver's virtual directory and there are even no class or codebehind
concepts at that time. The ASP.NET encaspulate the dynamic web page's
processing and provide such as webform framework, make the web page
developing most like desktop form developing. However, the fundamental
mechanism is still page/file/directory based, the vritual path rules is
very critical for such application.
In addition, as for using web.config/datastore to store the page
name, we still need to change two places when the page file changes ,
this is also limited by the fact that webform page file is not directly
1:1 associated with page class. Multi aspx page can associated with the
same code behind class and their name can vary.
Anyway, I still think using web.config to store the key/value pair is
a reasonable approach currently.
 
S

Steven Cheng[MSFT]

Hi Urs,

Thanks for your reply. As for the
====================
is there a way to get the aspx file
name and path of form2? I mean something such as

Sub DoRedirectToForm2()
Dim f as new form2
dim path as string = f.GetRelativeFileAndPath()
f.dispose
response.redirect(path)
end sub
====================

I'm afraid this is not possible in web application environment. As I've
mentioned in the former message , web application are all page based, and
the Page Class in asp.net is just simulating the program model in desktop
applicaiton, but the main actor in web application is still page. Also, the
aspx page file and .cs code behind class is not always 1:1 mapped.
Sometimes, we can use one single codebehind class for multi aspx pages,
other times some aspx page may even not use codebeind class(directly
embeded code in aspx ). That's why we can't retrieve the aspx page file's
name and location dynamically at runtime.

Thanks & Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,818
Latest member
Brigette36

Latest Threads

Top