简体   繁体   English

在一个解决方案中重定向多个项目

[英]Redirecting in multiple projects in one solution

In my C# Web Application, I have two different projects ("Project1" and "Project2") in one solution. 在我的C#Web应用程序中,一个解决方案中有两个不同的项目(“ Project1”和“ Project2”)。 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". 在“ Project1”中,我有一个带有按钮的网页,并且在OnClick事件上,我试图捕获会话变量并重定向到“ Project2”中名为“ Numbers.aspx”的网页。 I set "Project1" to include "Project2" as a dependency and "Project1" is the startup project. 我将“ Project1”设置为包括“ Project2”作为依赖项,并且“ Project1”是启动项目。 When I run "Project1" it goes to the following url: 当我运行“ Project1”时,它将转到以下URL:

http://localhost:5243/WebpageA.aspx

When I set "Project2" as the startup project and run it, it goes to the following url: 当我将“ Project2”设置为启动项目并运行它时,它将转到以下URL:

http://localhost:5571/Numbers.aspx

In "Project1" when I write: 在“ Project1”中,当我这样写:

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? 有人对我如何在其他项目中重定向到此网页并从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: 要重定向,我建议在您的应用设置中添加一个设置,该设置指定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. 这样,当需要部署到服务器时,您可以(希望)通过简单地将web.config中的“ Project2Host”设置更改为“ MyServer.com/Project2”或其他方式轻松地进行连接。


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"] . 重定向到目标URL加上"?Variable=" + Session["Variable"] Then pick it up in Project 2 using Session["Variable"] = Request.QueryString["Variable"]; 然后在项目2中使用Session["Variable"] = Request.QueryString["Variable"];拾取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. 脚注:如果您打算在两个站点之间共享数据,我会考虑将它们设置为同一Web应用程序项目下的单独文件夹/虚拟目录。

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

means that the framework searches for the Number.aspx within the scope of the project1. 表示框架在project1的范围内搜索Number.aspx。 It means starting with the root folder for Project1, search for the Numbers.aspx. 这意味着从Project1的根文件夹开始,搜索Numbers.aspx。

What you have to do is create a virtual directory for Project2 in IIS, and give the link of the path. 您要做的是在IIS中为Project2创建一个虚拟目录,并提供路径的链接。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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