5

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;
    }
4

2 に答える 2

4

リダイレクトするには、プロジェクト 2 のホストを指定する設定をアプリ設定に追加することをお勧めします。

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

リダイレクトについては、次のように記述できます。

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

そうすれば、サーバーにデプロイするときが来たら、web.config の "Project2Host" 設定を "MyServer.com/Project2" などに変更するだけで、(できれば) 簡単に接続できます。


セッション変数を転送する 2 番目の部分では、それをクエリ文字列として渡すことはできますか? ターゲット URL にリダイレクトします"?Variable=" + Session["Variable"]。次に、 を使用して Project 2 でピックアップしSession["Variable"] = Request.QueryString["Variable"];ます。


脚注: 2 つのサイト間でデータを共有する予定がある場合は、同じ Web アプリケーション プロジェクトの下に別のフォルダー/仮想ディレクトリとして設定することを検討します。

于 2013-07-18T00:31:21.320 に答える
1
Response.Redirect("~/Numbers.aspx") 

フレームワークが project1 のスコープ内で Number.aspx を検索することを意味します。これは、Project1 のルート フォルダーから開始して、Numbers.aspx を検索することを意味します。

IIS で Project2 の仮想ディレクトリを作成し、パスのリンクを指定する必要があります。

于 2013-07-18T00:30:05.993 に答える