2013-02-09 83 views
1

我可以在%tags, 之間的.Aspx文件中使用Request.ServerVariables["ALL_HTTP"],即<%=Request.ServerVariables["ALL_HTTP"]%> 但是.cs文件不會識別名稱空間。是什麼賦予了?我需要將這些變量放入我的代碼文件中的數組中。爲什麼不能在C#代碼文件中使用Request.ServerVariables [「ALL_HTTP」]?

+0

什麼給?可能會給出錯誤。那可能是什麼? – 2013-02-09 01:00:43

+0

@SimonWhitehead,「當前上下文中不存在名稱」請求「,即」不會識別名稱空間「。 – 2013-02-09 01:02:11

+2

你嘗試過'HttpContext.Current.Request'嗎? – 2013-02-09 01:14:32

回答

2
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web; 
using System.Web.UI; 
using System.Web.UI.WebControls; 

using System.Collections.Specialized; 

public partial class _Default : System.Web.UI.Page 
{ 
    protected void Page_Load(object sender, EventArgs e) 
    { 

     NameValueCollection coll; 
     coll = Request.ServerVariables; 
     // Get names of all keys into a string array. 
     String[] arr1 = coll.AllKeys; 
     for (int loop1 = 0; loop1 < arr1.Length; loop1++) 
     { 
      Response.Write("Key: " + arr1[loop1] + "<br>"); 
      String[] arr2 = coll.GetValues(arr1[loop1]); 
      for (int loop2 = 0; loop2 < arr2.Length; loop2++) 
      { 
       Response.Write("Value " + loop2 + ": " + Server.HtmlEncode(arr2[loop2]) + "<br>"); 
      } 
     } 


    } 
} 
相關問題