Asp.net null rewrite object reference

I developed my asp.net site in .NET 2.0 on another system where it works fine. Now that I have copied the asp.net site on my system and started it, I get a runtime error:

The reference to the object is not set to the instance of the 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 get an object link error in the line:

 context.CompleteRequest(); 

In my web.Config file there is

 <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 problem?

EDIT Change 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"); } } 
+4
source share
3 answers

I strongly suspect that you will want to put endterequest at the end of the context_beginrequest method, because now it does not make sense. If it is not, send this method so that it can understand what you are trying to do.

EDIT: Looks like you intend 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 want to call CompleteRequest if you are actually not doing anything in BeginRequest. And to be clear, in your source code, you call CompleteRequest before the BeginRequest event even fires.

+4
source

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

This usually means stopping the request, but you call it when your application is initialized and the requests are not processed. I assumed that in .NET 2.0 it will endure this call and do nothing wrong, but in a later version it explodes.

It doesn't seem to me that you want to stop the request right after you rewrote the URLs ... otherwise, why rewrite them? So just try to get rid of the method call.

0
source

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

Source: https://habr.com/ru/post/1346076/


All Articles