问题
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.
回答1:
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
回答2:
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.
回答3:
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.
来源:https://stackoverflow.com/questions/1786980/redirection-between-two-websites-within-one-solution-asp-net-vs-2008-iis-5