在C#中使用visual studio 2010 .NET framework 4.0。
每次尝试使用response.redirect("url")
进行重定向时,都会出现以下错误:
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Web_uck3gvsb
StackTrace:
at _Default.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\p1\Default.aspx.cs:line 15
at System.Web.Util.CalliHelper.EventArgFunctionCaller(IntPtr fp, Object o, Object t, EventArgs e)
at System.Web.Util.CalliEventHandlerDelegateProxy.Callback(Object sender, EventArgs e)
at System.Web.UI.Control.OnLoad(EventArgs e)
at System.Web.UI.Control.LoadRecursive()
at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)
InnerException:
以下是代码:即使是简单的没有任何东西,我也会得到同样的错误!
using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
public HttpResponse response;
protected void Page_Load(object sender, EventArgs e)
{
response.Redirect("http://www.google.com");
}
}
答案 0 :(得分:6)
您应该使用:
Response.Redirect("www.bing.com");
不
response.Redirect("...")
答案 1 :(得分:2)
您需要使用Page.Response属性:
Page.Response.Redirect(...);
或只是
Response.Redirect(...);
声明HttpResponse
对象时,它未初始化,因此null
。这就是例外的原因。
答案 2 :(得分:2)
注释掉您的public HttpResponse response;
您声明了一个未初始化的unnecessary
变量。
public partial class _Default : System.Web.UI.Page
{
//public HttpResponse response;
protected void Page_Load(object sender, EventArgs e)
{
//this is enough
Response.Redirect("http://www.google.com");
}
}