对象引用null- URL重写asp.net
我在.NET 2.0中开发了我的asp.net网站,在其他系统中工作正常。现在,当我抄在我的系统的asp.net网站并运行它比我收到运行时错误:对象引用null- URL重写asp.net
context.CompleteRequest();
:
Object reference not set to an instance of an object.
public class FixURLs : IHttpModule
{
public FixURLs()
{
}
#region IHttpModule Members
public void Dispose()
{
// do nothing
}
public void Init(HttpApplication context)
{
context.BeginRequest += new EventHandler(context_BeginRequest);
context.CompleteRequest();
}
..... some other logic
我在该行获得对象引用错误
我的web.config文件有
<compilation debug="true">
<assemblies>
<add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
</assemblies>
</compilation>
我该如何解决这个问题?
编辑 编辑注新的代码添加
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Request.RawUrl.ToLower().Contains("/bikes/default.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "");
}
else if (app.Request.RawUrl.ToLower().Contains("/bikes/mountainbike.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "ItemID=1");
}
}
我强烈怀疑,你会想要把completerequest在context_beginrequest方法结束,因为现在这并没有真正意义。如果情况并非如此,请发布该方法,这很清楚你正在尝试做什么。
编辑:它看起来像你的目的是要做到这一点:
void context_BeginRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
if (app.Request.RawUrl.ToLower().Contains("/bikes/default.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "");
app.CompleteRequest();
}
else if (app.Request.RawUrl.ToLower().Contains("/bikes/mountainbike.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "ItemID=1");
app.CompleteRequest();
}
}
它看起来并不像你想打电话给CompleteRequest除非你是真的在做事情中的BeginRequest。并且要清楚的是,在您的原始代码中,您在BeginRequest事件甚至触发之前调用CompleteRequest。
这就是我要发表的意见:) – 2011-04-06 06:05:20
我已经更新了代码,请检查 – Chris 2011-04-06 06:40:10
我想你应该刚刚离开了您的来电context.CompleteRequest();
这通常意味着停止执行请求的,但你要调用它,当您的应用程序初始化并没有请求正在处理中。我的猜测是,在.NET 2.0中,它可以容忍这个呼叫,并且不会做任何坏事,但在后来的版本中它会爆炸。
它没有在我看来像你想要在重写URL之后立即停止请求......否则,为什么要重写呢?所以试着摆脱那个方法调用。
无效context_BeginRequest(对象发件人,EventArgs的){
HttpApplication app = (HttpApplication)sender;
if (app.Request.RawUrl.ToLower().Contains("/bikes/default.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "");
app.CompleteRequest();
}
else if (app.Request.RawUrl.ToLower().Contains("/bikes/mountainbike.aspx"))
{
app.Context.RewritePath("BikeInfo.aspx", "", "ItemID=1");
app.CompleteRequest();
}
}
任何帮助,将不胜感激 – Chris 2011-04-02 07:20:46
你在本地IIS或卡西尼开发Web服务器上运行呢? – 2011-04-06 05:54:19
我需要在context_BeginRequest方法中看到代码。你能发布代码吗? – 2011-04-06 06:00:27