简体   繁体   中英

Redirecting in multiple projects in one solution

In my C# Web Application, I have two different projects ("Project1" and "Project2") in one solution. In "Project1" I have a webpage with a button and on the OnClick event I am trying to capture a session variable and redirect to a webpage in "Project2" called "Numbers.aspx". I set "Project1" to include "Project2" as a dependency and "Project1" is the startup project. When I run "Project1" it goes to the following url:

http://localhost:5243/WebpageA.aspx

When I set "Project2" as the startup project and run it, it goes to the following url:

http://localhost:5571/Numbers.aspx

In "Project1" when I write:

Response.Redirect("~/Numbers.aspx");

it says the path is not valid. I also tried the following path and it says it does not exist:

 Response.Redirect("~/Project2/Numbers.aspx");

Does anyone have any suggestions how I can redirect to this webpage in the different project and capture a session variable from Project1? Thanks!

   //OnClick event
    protected void click(object sender, EventArgs e)
    {
        Session["Variable"] = textBoxName.Text;
    }

To redirect, I suggest adding a setting in your app settings, that specifies the host of Project 2:

<appSettings>
    <add key="Project2Host" value="localhost:5571/" />
</appSettings>

Now for the redirect, you can write:

Response.Redirect(Request.Url.Scheme + "://" 
    + ConfigurationManager.AppSettings["Project2Host"] 
    + "Numbers.aspx");

That way, when it comes time to deploy to a server, you can (hopefully) easily wire it up by simply changing the "Project2Host" setting in the web.config to "MyServer.com/Project2", or whatever.


For the second part, transferring a session variable, maybe you could pass it as a query string? Redirect to the target URL plus "?Variable=" + Session["Variable"] . Then pick it up in Project 2 using Session["Variable"] = Request.QueryString["Variable"]; .


As a footnote: if you're planning to share data between the two sites, I'd consider setting them up as separate folders/virtual directories under the same web application project.

Response.Redirect("~/Numbers.aspx") 

means that the framework searches for the Number.aspx within the scope of the project1. It means starting with the root folder for Project1, search for the Numbers.aspx.

What you have to do is create a virtual directory for Project2 in IIS, and give the link of the path.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM