views:

49

answers:

3

Hi! I have an issue with redirecting from one asp website to another within one VS solution. I have set up virtual directories as follows: C:\WebSites\Website1 - /Website1 C:\WebSites\Website2 - /Website2 My starting website is Website1. I want to redirect user to Website2. I use Response.Redirect("/Website2/Default.aspx") and get 404 error. What am I doing wrong? Any advices are highly appreciated! Thank you in advance.

A: 

When you have two virtual directories, if they are configured to be their own applications, then their individual roots are considered THE root, and you will have to redirect to a full URL.

Josh Pearce
Thank you for the reply. Can I reconfigure apps anyhow differently so to be able to use virtual path?
Olga
Well, you could try Response.Redirect("~/../Website2/Default.aspx"), but I have no idea if ti will work.
Josh Pearce
+1  A: 

Lets say the current page is

   http://localhost/Website1/Default.aspx,

if you do a Redirect like this

   Response.Redirect("/Website2/Default.aspx")

in there, the URL you are redirecting is

    http://localhost/Website1/WebSite2/Default.aspx

which don't exists.

You have to redirect to a full URL instead of a relative URL.

Something like
Response.Redirect("http://localhost/Website2/Default.aspx")

Hope it Helps

Limo Wan Kenobi
Thank you for your post. I tried to use http://127.0.0.1/Survey/Default.aspx and it works but this is not something I want to have hard coded. Is there any possibility to use virtual path?
Olga
I would add a key to the appSetting of the config file with the URL of the server of the other application and configure it under deployment. Something like <appSettings> <add key="otherServerURL" value="http: //localhost/" /> </appSettings> and do Response.Redirect(ConfigurationManager.AppSettings["otherServerURL"] + "Website2/Default.aspx"); Maybe someone here can give us another answer.
Limo Wan Kenobi
+1  A: 

You could always pick the server name out of the HttpRequest.Url or HttpRequest.ServerVariables objects.

Response.Redirect(string.Format("http://{0}/WebSite2/default.aspx", 
                                Request.Url.Host));

Or

Response.Redirect(string.Format("http://{0}/WebSite2/default.aspx", 
                                Request.ServerVariables["HTTP_HOST"]));

Which will save you hardcoding the server name in the redirects.

Zhaph - Ben Duguid
Thank you a lot. This is working.
Olga