4

我在其他运行良好的系统中使用 .NET 2.0 开发了我的 asp.net 网站。现在,当我在系统中复制 asp.net 网站并运行它时,我得到了运行时错误:

你调用的对象是空的。

 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");
    }
 }
4

3 回答 3

4

我强烈怀疑您可能希望将 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(); 
    }
 }

除非您实际上是在 BeginRequest 中做某事,否则您似乎不想调用 CompleteRequest。并且要清楚,在您的原始代码中,您在 BeginRequest 事件甚至触发之前调用 CompleteRequest。

于 2011-04-06T06:01:28.597 回答
0

我认为你应该忽略你的电话context.CompleteRequest();

这通常是为了停止请求的执行,但是当您的应用程序正在初始化并且没有请求正在处理时,您会调用它。我的猜测是,在 .NET 2.0 中它会容忍这个调用并且不会做任何坏事,但在以后的版本中它会崩溃。

在我看来,您并不想在重写 URL 后立即停止请求……否则,为什么还要重写它们?因此,只需尝试摆脱该方法调用。

于 2011-04-12T18:40:10.053 回答
0

void context_BeginRequest(对象发送者,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(); 
}
}
于 2017-01-02T17:03:20.017 回答