当前位置:编程学习 > C#/ASP.NET >>

帮忙看看啊。编译时报错,错误 当前上下文中不存在名称“Request” "Response" "Server"

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;

public partial class Default2
{
   public static void Http_get()

   {
   int loop1, loop2;

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

   }

}

 编译后,帮忙看看啊。编译时报错,错误 当前上下文中不存在名称“Request” "Response" "Server"

多谢。 --------------------编程问答-------------------- 好像没有继承Page类 --------------------编程问答--------------------
引用 1 楼 yuxh81 的回复:
好像没有继承Page类


--------------------编程问答-------------------- public partial class Default2: System.Web.UI.Page
加上这个试试 --------------------编程问答-------------------- 加上System.Web.HttpContext.Current.前缀 --------------------编程问答-------------------- 或者将Rsponse,Request,Server替换:
HttpContext.Current.Response
HttpContext.Current.Request
HttpContext.Current.Server --------------------编程问答--------------------
引用 1 楼 yuxh81 的回复:
好像没有继承Page类

请问,一定要继承吗?

我先new 一个 Page 实例,然后再引用行吗?

如下 

using System;
using System.Collections.Specialized;
using System.Web;
using System.Web.UI;

public partial class Default2

{
   public static void Http_get()

   {
       Page p1 = new Page();       int loop1, loop2;

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

   }

}

这样可行吗?  为什么一定要继承? Page 大部份的成员都是非静态成员。 --------------------编程问答-------------------- 可以啊,但是没必要啊,直接继承比较方便,不知道你为什么要自己设置属性 --------------------编程问答-------------------- 这样的话,那你还不如用第二种方案啊! --------------------编程问答--------------------
引用 3 楼 yuxh81 的回复:
public partial class Default2: System.Web.UI.Page
加上这个试试


比较好

--------------------编程问答--------------------
引用 5 楼 yuxh81 的回复:
或者将Rsponse,Request,Server替换:
HttpContext.Current.Response
HttpContext.Current.Request
HttpContext.Current.Server


能解释一下吗,你这种办法。 谢谢。 --------------------编程问答-------------------- 在类中添加web引用使用HttpContext.Current
HttpContext.Current
为当前 HTTP 请求获取或设置 HttpContext 对象 --------------------编程问答--------------------
引用 11 楼 wuyq11 的回复:
在类中添加web引用使用HttpContext.Current
HttpContext.Current
为当前 HTTP 请求获取或设置 HttpContext 对象


谢谢!

理解了。

请问,HttpContext.Current 在这里是为当前的HTTP请求设置 httpcontext对象,那么理论上可以为其它指定的HTTP请求设置httpcontext对象吗,或者说是否只能为当前HTTP请求设置httpcontext对象?会存在为其它http请求设置httpcontext对象的说法吗?  谢谢。
补充:.NET技术 ,  ASP.NET
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,