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

12,609

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.

Share:
12,609
Sean Anderson
Author by

Sean Anderson

Sr. Software Developer and blockchain enthusiast w/ ~8 years of full-stack experience in JavaScript and C#. Extensive open-source contributions and a part of the MarionetteJS organization. Currently full-time day trader.

Updated on June 04, 2022

Comments

  • Sean Anderson
    Sean Anderson almost 2 years

    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!