c# - 为什么我不能在 C# 代码文件中使用 Request.ServerVariables ["ALL_HTTP"]?

标签 c# asp.net iis

我可以使用Request.ServerVariables["ALL_HTTP"]在 .Aspx 文件中 % 标记之间, 即<%=Request.ServerVariables["ALL_HTTP"]%> 但 .cs 文件将无法识别 namespace 。是什么赋予了?我需要将这些变量放入代码文件中的数组中。

最佳答案

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>");
            }
        }


    }
}

关于c# - 为什么我不能在 C# 代码文件中使用 Request.ServerVariables ["ALL_HTTP"]?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14783756/

相关文章:

asp.net - 我可以在 SQL session 状态配置中重用现有连接字符串吗?

c# - 使用where条件连接多个表

c# - 无法读取配置部分 'configsections',因为它缺少部分声明

c# - 将 HttpListener 转换为在 IIS 中运行

c# - 将自定义属性添加到 ASMX Web 服务方法

c# - 在设计/构建时检查自定义属性参数

c# - 用 2 个数字之间的所有数字填充下拉列表

c# - 在另一台机器上运行我的 Asp.Net Web 应用程序

c# - 泛型 C# 中的错误!

C# - 使用计时器重复方法调用