2012年10月30日 星期二

ASP.NET (C#) 取得主機目前網址列字串值

from http://www.dotblogs.com.tw/wesley0917/archive/2011/01/13/20786.aspx



01using System;
02 
03public partial class Default3 : System.Web.UI.Page
04{
05    //當頁面正在載入時所要執行動作。
06    protected void Page_Load(object sender, EventArgs e)
07    {
08        //宣告字串變數。
09        string strThisURL = null;
10        //當 GET 傳入參數不是無值時。
11        if (string.IsNullOrEmpty(Request.QueryString["URL"]))
12        {
13            //取得目前伺服器網址全部字串。輸出:http://localhost/Default.aspx
14            strThisURL = "http://" + Request.ServerVariables["Server_Name"] + Request.ServerVariables["URL"] + "<br />";
15            //取得目前伺服器網址主機名稱字串。輸出:http://localhost
16            strThisURL += "http://" + Request.ServerVariables["Server_Name"];
17        }
18        else //有 GET 傳入參數值時。
19        {
20            //取得目前伺服器網址全部字串與 GET 參數字群值。輸出:http://localhost/Default.aspx?User=mary
21            strThisURL = "http://" + Request.ServerVariables["Server_Name"] + Request.ServerVariables["URL"] + "?User=" + Request.QueryString["URL"];
22        }
23        //輸出結果。
24        Response.Write(strThisURL);
25    }
26}


 

MangoHost Copyright © 2009 Cookiez is Designed by Ipietoon for Free Blogger Template