我已经在其他系统中使用.NET 2.0开发了我的asp.net网站,它运行良好。现在,当我在系统中复制 asp.net 网站并运行它时,我收到了运行时错误:
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
我在以下行收到对象引用错误:
context.CompleteRequest();
我的 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。
关于asp.net - 对象引用 null - URL 重写 asp.net,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5502158/