Response.Redirect from one web project to another in Visual Studio

青春壹個敷衍的年華 提交于 2020-01-11 03:44:43

问题


I am attempting to integrate a project into a pre-existing solution.

The start-up project in the solution is named "Foo." It is written to the virtual path "/csweb/." When this project starts up it loads /csweb/Default.aspx. This is the current, unmodified home page.

I am attempting to redirect to a different home page in a different project.

I added a second project to the solution named "Bar." Under its project properties I told it to write to the virtual path "/csweb/." It is not marked as the start-up project, nor are there multiple start-up projects -- just Foo. Bar's homepage is accessible when Bar is marked as the start-up project. It's homepage is represented by localhost:port/csweb/Default.aspx

I would like to redirect to Bar's Default.aspx while using Foo as the start-up project. I am fine with renaming Bar's Default.aspx to something non-ambiguous, if that would make the problem easier.

If I try HttpContext.Response.Redirect("~/Default.aspx") then it clearly takes me to Foo/Default.aspx and not Bar/Default.aspx, but the url in the browser is just localhost:port/csweb/Default.aspx. There is no indication of being inside of a project, completely understandable. If I rename Bar/Default.aspx to Bar/UniqueName.aspx and attempt HttpContext.Response.Redirect("~/UniqueName.aspx") then the page is not found.

What's the right call here? Do I need to setup Bar as a pre-existing virtual directory in IIS so that I can specify a more complete path name to avoid ambiguity? Or do I need to do something fancy with multiple start-up web projects in order for it to be accessible?

Thanks for any advice on this confusing issue.

EDIT: I ended up just adding another folder to Foo's project and moving the contents of Bar's project to this folder. I am then able to do HttpContext.Response.Redirect("~/Bar/Bar's Default.aspx") I'd still love to hear a better solution than this, though!


回答1:


Try HttpContext.Response.Redirect("/csweb/Default.aspx"). The tilde "~" in your current redirect says to start in this application's home folder. By removing that you can redirect to some other location on your site.

You can also fully qualify the redirect to go to an entirely different site if you wish.



来源:https://stackoverflow.com/questions/5669159/response-redirect-from-one-web-project-to-another-in-visual-studio

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