4

I have developed my asp.net website in .NET 2.0 in other system where it is working fine. Now when I copied the asp.net website in my system and run it than I am getting the run time error:

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

I am getting object reference error at the line:

context.CompleteRequest();

My web.Config file has

<compilation debug="true">
  <assemblies>
    <add assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"/>
  </assemblies>
</compilation>

How can I fix this issue?

EDIT Edit Note New code added

 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");
    }
 }
Chris
  • 2,293
  • 11
  • 48
  • 86
  • Any help would be appreciated – Chris Apr 02 '11 at 07:20
  • Are you running this on local IIS or Cassini dev web server? – Elad Lachmi Apr 06 '11 at 05:54
  • I need to see the code in the context_BeginRequest method. Can you post the code? – Elad Lachmi Apr 06 '11 at 06:00
  • @Elad Lachmi I am running this application with Visual studio 2005 from one machine and it is running fine, but when I copy paste the code to other machine and run it in the Visual Studio 2010 I am getting this error. – Chris Apr 06 '11 at 06:39
  • @Chris - My guess is Cassini version in VS2010 is showing the right behavior to your code. CompleteRequest halts all processing of the request. There is not sense in deligating a handler and then droping the request on it. Passing control to a handler is not like calling a method. It's not like the handler is called and then control comes back to the calling method. You need to complete the request inside the context_BeginRequest method. – Elad Lachmi Apr 06 '11 at 06:45
  • @Elad Lachmi So Do I need to put context.CompleteRequest(); in void context_BeginRequest(object sender, EventArgs e) instead of public void Init(HttpApplication context) ? – Chris Apr 06 '11 at 06:50
  • If you want to stop the request, then yes. – Elad Lachmi Apr 06 '11 at 07:13
  • @Chris: Could you share the stack trace (including inner stack) corresponding to the Object reference exception – Naraen Apr 07 '11 at 01:54

3 Answers3

4

I strongly suspect that you would want to put completerequest at the end of the context_beginrequest method because right now this doesn't really make sense. If that isn't the case please post that method as well so it's clear what you are trying to do.

EDIT: It looks like your intention is to do this:

 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(); 
    }
 }

It doesn't look like you'd want to call CompleteRequest unless you are actually doing something in BeginRequest. And to be clear, in your original code, you are calling CompleteRequest before the BeginRequest event even fires.

Jarrett Widman
  • 6,329
  • 4
  • 23
  • 32
0

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(); 
}
}
Panther
  • 3,312
  • 9
  • 27
  • 50
0

I think you should just leave out your call to context.CompleteRequest();

This is normally meant to stop the execution of a request, but you're calling it when your application is initializing and no requests are being processed. My guess was that in .NET 2.0 it would tolerate this call and not do anything bad, but in a later version it blows up.

It doesn't look to me like you want to stop the request immediately after you've rewritten the URLs... otherwise, why even rewrite them? So just try getting rid of that method call.

RandomEngy
  • 14,931
  • 5
  • 70
  • 113