Redirection between two websites within one solution (ASP.NET + VS 2008 + IIS 5.1)

六月ゝ 毕业季﹏ 提交于 2020-01-05 13:55:16

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!