C# 简单代理asp.net错误

C# 简单代理asp.net错误,c#,asp.net,error-handling,compiler-errors,runtime-error,C#,Asp.net,Error Handling,Compiler Errors,Runtime Error,我不知道为什么。aspx给我: 下载提示,讨厌的下载 我不知道怎么了 我的代码: <%@ Page Language="C#" %> <script runat="server"> protected void Page_Load(object sender, EventArgs e) { var strURL = "http://www.google.com"; System.Net.WebRespo

我不知道为什么。aspx给我:

下载提示,讨厌的下载

我不知道怎么了

我的代码:

  <%@ Page Language="C#" %>


<script runat="server">
        protected void Page_Load(object sender, EventArgs e)
      {
        var strURL = "http://www.google.com";

        System.Net.WebResponse objResponse = default(System.Net.WebResponse);
        System.Net.WebRequest objRequest = default(System.Net.WebRequest);
        string result = null;
        objRequest = System.Net.HttpWebRequest.Create(strURL);
        objResponse = objRequest.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream());
        result = sr.ReadToEnd();
        //clean up StreamReader 
        sr.Close();

        //WRITE OUTPUT
        Response.ContentType = "application/html";
        Response.Write(result);//""
        Response.Flush();//""

    }
</script>

受保护的无效页面加载(对象发送方、事件参数e)
{
var strURL=”http://www.google.com";
System.Net.WebResponse objResponse=默认值(System.Net.WebResponse);
System.Net.WebRequest objRequest=默认值(System.Net.WebRequest);
字符串结果=null;
objRequest=System.Net.HttpWebRequest.Create(strURL);
objResponse=objRequest.GetResponse();
System.IO.StreamReader sr=新的System.IO.StreamReader(objResponse.GetResponseStream());
结果=sr.ReadToEnd();
//清理StreamReader
高级关闭();
//写入输出
Response.ContentType=“应用程序/html”;
响应。写入(结果);/“”
Response.Flush();/“”
}
我还是不知道怎么了

不能有“全局”代码,必须将其封装在方法中

比如:

<%@ Page Language="C#" %>

<script runat="server">
    protected void Page_Load( object sender, EventArgs e )
    {
        // Your code...
    }
</script>
对您使用的所有名称空间重复此
指令。或者使用类的完整限定名,包括名称空间,例如
System.Net.WebResponse

第二次更新:

好的,这只是一个猜测,以消除(其中一个?)逻辑错误:

而不是:

var strURL = context.Server.UrlDecode(context.Request["http://www.google.com"]);
写:

var strURL = "http://www.google.com";
然后看看它是否工作得更好(根据您使用的评论)

第三次(最终?)更新:


如果这也没有帮助,请尝试在Visual Studio(Express)中调试它,并设置断点、单步执行代码、检查变量等。

使用单文件页模型有两个问题阻止代码工作。页面加载方法签名错误,且上下文已存在,未传入。将代码更新为:

<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
  {
        var strURL = "http://www.google.com";

        System.Net.WebResponse objResponse = default(System.Net.WebResponse);
        System.Net.WebRequest objRequest = default(System.Net.WebRequest);
        string result = null;
        objRequest = System.Net.HttpWebRequest.Create(strURL);
        objResponse = objRequest.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream());
        result = sr.ReadToEnd();
        //clean up StreamReader 
        sr.Close();

        //WRITE OUTPUT
        Response.ContentType = "text/html";//remove context
        Response.Write(result);//""

    }
</script>

受保护的无效页面加载(对象发送方、事件参数e)
{
var strURL=”http://www.google.com";
System.Net.WebResponse objResponse=默认值(System.Net.WebResponse);
System.Net.WebRequest objRequest=默认值(System.Net.WebRequest);
字符串结果=null;
objRequest=System.Net.HttpWebRequest.Create(strURL);
objResponse=objRequest.GetResponse();
System.IO.StreamReader sr=新的System.IO.StreamReader(objResponse.GetResponseStream());
结果=sr.ReadToEnd();
//清理StreamReader
高级关闭();
//写入输出
Response.ContentType=“text/html”;//删除上下文
响应。写入(结果);/“”
}

@user131008我已经帮你删除了编译错误,我无法帮你删除逻辑错误;-)从这里@user131008好的,我已经更改了我的答案。@user131008-IE9工作正常。请尝试其他浏览器/版本。然后告诉我们您使用的版本。您可能需要在旧IE(大约7个及以上)上使用一些技巧来更新contentType,Firefox和Chrome也是如此!只是Chrome不会问你是否要下载@user131008-我更新了ContentType。再试一次,应该可以。Text/html是默认值,因此如果愿意,您可以实际删除该行。
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
  {
        var strURL = "http://www.google.com";

        System.Net.WebResponse objResponse = default(System.Net.WebResponse);
        System.Net.WebRequest objRequest = default(System.Net.WebRequest);
        string result = null;
        objRequest = System.Net.HttpWebRequest.Create(strURL);
        objResponse = objRequest.GetResponse();
        System.IO.StreamReader sr = new System.IO.StreamReader(objResponse.GetResponseStream());
        result = sr.ReadToEnd();
        //clean up StreamReader 
        sr.Close();

        //WRITE OUTPUT
        Response.ContentType = "text/html";//remove context
        Response.Write(result);//""

    }
</script>