BUG? With A HTTPModule

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

I have come across a very strange bug while working with an HTTPModule.
The bug appears to manifest itself when in the context_BeginRequest
making a call to context.Request.QueryString["SomeItem"] when no items are
specified.
Example code is
try
{
if (context.Request.QueryString["AnyKey"]!=null) { ... do some code
with it; }
} catch (Exception ex) { ... handle error here which never gets touched
because no error is thrown }

Down Below We have our Actual
context.Context.RewritePath(ActualUrl);

Now Stepping through the code line by Line I see that if we query with
Bleah.aspx?anyvalue=1
it goes through the above code and works As expected
however if i go bleah.aspx without it it goes through the code and then gets
done. But when the browser comes back even though the actual URL rewritten
is the same both times it comes back with a 404 Error.
in the "Requested URL" field it shows the same URL. and in the ActualUrl (in
debug mode) both are exactly the same.
The only difference is if no Parameters were added via the original URL and
a Request Is attempted for them it results in a 404
bleah.aspx?id=101 goes to this.aspx - Works fine - Even though were were
asking for the field AnyKey and not id
bleah.aspx goes to this.aspx - 404

This is occurring on an XP Pro System. I have not even dared to move the
project over to the live server as it is so big and i've spent the last 5
hours pinpointing exactly where and what the problem is
 
try {
if (context.Request==null)
{
return;
}
// If Any one of the next 3 lines are Called WITHOUT ANY PARAMETERS ACTUALL SPECIFIED Even though it NEver throws an error and continues execution normally it 404s the request even though the destination URL is EXACTLY the same
if (context.Request.QueryString.Count==0) {}
if (context.Request.QueryString["ItemCode"]!=null) { NewUrl.Append("&ItemCode="). Append(context.Request.QueryString["ItemCode"]); }
if (context.Request.QueryString["ItemQuantity"]!=null) { NewUrl.Append("&ItemQuantity="). Append(context.Request.QueryString["ItemQuantity"]); }
}
catch (Exception ex)
{
context.Response.Write(ex.ToString());
context.Context.RewritePath("WebForm1.aspx");
}
 
Hi,

From your descritption, you used a HttpModule to perform Url ReWriting
according to the querystring in the original requested url(checking them in
the BeginRequest event). However, you found this worked when the certain
querystring is specified but encountered 404 error if request the url
without any querystring such as somepage.aspx , yes?

As for this issue, I've some further questions on it:
1. Are the pages you encountered problem in a IIS server's root folder or
in a Virtual directory?

2. You may have a try comment the "Context.RewritePath" code line and
replace it by the
Response.Write(...)
Response.End()
to see whether this can work?

3. Does the problem occur if you hard code the "newUrl" which used in the
Context.RewritePath? And since when 404 error occur when processing a aspx
page, the error info will contains the certain actually requested page's
path such as below:
===============================
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make sure that
it is spelled correctly.

Requested Url: /FormAuthApp/controls/WebForm1.aspx
====================================
Have you checked the actual path displayed in the above's "Requested Url"
to see whether it is a invalid path or is a existing page path?

In addition, I've generated a simple test on my side, on WIN XP PRO
machine, with ASP.NET1.1 and IIS5, the web application is in a certain
virtual directory. I added a simple HTTPModule as below:
=============================
public class RedirectModule : IHttpModule
{
private HttpApplication httpApp;

public void Init(HttpApplication httpApp)
{
this.httpApp = httpApp;
httpApp.BeginRequest += new EventHandler(OnBeginRequest);
}

void OnBeginRequest(object sender, EventArgs a)
{
HttpApplication application = (HttpApplication)sender;
HttpRequest request = application.Context.Request;
HttpResponse response = application.Context.Response;
string newUrl = "WebForm1.aspx";

try
{
if (request.QueryString["id"]!=null)
{
newUrl = newUrl + "?id=" + request.QueryString["id"];
}
}catch (Exception ex)
{
response.Write("<br>"+ ex.Message);
}

application.Context.RewritePath(newUrl);

}

public void Dispose()
{}
}

I tried visit some pages (which is in the root folder of the application)
both with querystring and without any querystring but seems not encounter
the problem. So I think it'll be more helpful if you can provide a detailed
repro description , for example:
Applicaiton's virutal folder path:
page position, complete code in the HTTPModule and steps to repro the
problem.

Thus, I'll be able to do some futher research on my side. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
That is pretty close to what I am doing.
its in a Virtual Colder
the Bin directory is in
C:\inetpub\wwwroot\CompanyName\bin
The URL I am accessing is located in or http://localhost:8080/CompanyName/Shopping/Bleah.aspx
C:\inetpub\wwwroot\CompanyName\Shopping\

The sample you provided is pretty close except that my sample URL looks like
Basepath = "/CompanyName/"
I then append "Shopping/" onto it based on some logic.
Then after that I look to see if There is anything in the QueryString
and if there is I copy it over to the url
So in the end my Url should look like
BasePath/CompanyName/Shopping/SomeFile.aspx?itemid=...
or
BasePath/CompanyName/Shopping/AnotherFile.aspx?

I am not sure how you were able to Response.Write or Request.QueryString without referencing them off of application though as I have to use
context.Request.QueryString instead of your > newUrl = newUrl + "?id=" + request.QueryString["id"];

But aside from that minor discrepancy the end results is virtually the same. I've Set Breakpoints at entry and end point and verified that the URL's are the same
The only difference being that one had a check for the context.Request.QueryString and that 404's and that the other one does not.



Steven Cheng said:
Hi,

From your descritption, you used a HttpModule to perform Url ReWriting
according to the querystring in the original requested url(checking them in
the BeginRequest event). However, you found this worked when the certain
querystring is specified but encountered 404 error if request the url
without any querystring such as somepage.aspx , yes?

As for this issue, I've some further questions on it:
1. Are the pages you encountered problem in a IIS server's root folder or
in a Virtual directory?

2. You may have a try comment the "Context.RewritePath" code line and
replace it by the
Response.Write(...)
Response.End()
to see whether this can work?

3. Does the problem occur if you hard code the "newUrl" which used in the
Context.RewritePath? And since when 404 error occur when processing a aspx
page, the error info will contains the certain actually requested page's
path such as below:
===============================
Description: HTTP 404. The resource you are looking for (or one of its
dependencies) could have been removed, had its name changed, or is
temporarily unavailable. Please review the following URL and make sure that
it is spelled correctly.

Requested Url: /FormAuthApp/controls/WebForm1.aspx
====================================
Have you checked the actual path displayed in the above's "Requested Url"
to see whether it is a invalid path or is a existing page path?

In addition, I've generated a simple test on my side, on WIN XP PRO
machine, with ASP.NET1.1 and IIS5, the web application is in a certain
virtual directory. I added a simple HTTPModule as below:
=============================
public class RedirectModule : IHttpModule
{
private HttpApplication httpApp;

public void Init(HttpApplication httpApp)
{
this.httpApp = httpApp;
httpApp.BeginRequest += new EventHandler(OnBeginRequest);
}

void OnBeginRequest(object sender, EventArgs a)
{
HttpApplication application = (HttpApplication)sender;
HttpRequest request = application.Context.Request;
HttpResponse response = application.Context.Response;
string newUrl = "WebForm1.aspx";

try
{
if (request.QueryString["id"]!=null)
{
newUrl = newUrl + "?id=" + request.QueryString["id"];
}
}catch (Exception ex)
{
response.Write("<br>"+ ex.Message);
}

application.Context.RewritePath(newUrl);

}

public void Dispose()
{}
}

I tried visit some pages (which is in the root folder of the application)
both with querystring and without any querystring but seems not encounter
the problem. So I think it'll be more helpful if you can provide a detailed
repro description , for example:
Applicaiton's virutal folder path:
page position, complete code in the HTTPModule and steps to repro the
problem.

Thus, I'll be able to do some futher research on my side. Thanks.


Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Hi,

Thanks for your reply. As you mentioned that you use some certain string to
concate the whole new url which is used to rewrite the Requested path. Have
you also tried a fixed path, for example:
put a test page named test.aspx in the application's root folder and change
the rewritepath code as below:
void OnBeginRequest(object sender, EventArgs a)
{
HttpApplication application = (HttpApplication)sender;
.......................// check the querystring
.......................

application.Context.RewritePath("http://localhost:8080/CompanyName/test.aspx
");
}

Or you can try the "~/....aspx" url, the "~/" means begin from the
application's root folder(virutal directory) which can help specify the
application's root directory without hardcode it. If the above test still
failed, I'm afraid that it would require intensive troubleshooting. You may
need to submit a service request to
our Product Support Service. We may do some dump analysis on it and see why
the exception happens. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
I have tried that and it has ended up with the same results.
Below is the code I am using to attempt to avoid this from happening but if
a user were to spoof it and access the one page without any properties it
404's

string TmpMode = UrlSegments[1].Replace(".aspx","").ToLower();
NewUrl.Append("Payment/ShoppingCart.aspx"). Append("?mode=").
Append(TmpMode);
if (TmpMode=="addtocart")
{
if (context.Request.QueryString["ItemCode"]!=null) {
NewUrl.Append("&ItemCode=").
Append(context.Request.QueryString["ItemCode"]); }
if (context.Request.QueryString["ItemQuantity"]!=null) {
NewUrl.Append("&ItemQuantity=").
Append(context.Request.QueryString["ItemQuantity"]); }
}

the If statement is for all the other pages that are accessed that do not
have variables however if they are addingtocart and they dont specify any
values it will 404 as expected. I am currentlyl going to live with it and
then monitor how many people actually access it without the variables. it
"should" be none but you never know with people ;)

As far as i can tell I am as stumped as everybody. The Project is going to
go live in the next week or 2 at which point i should be able to see if the
problem also happens on a Windows 2003 Server. If the problem persists on
Windows 2003 server also and the overall deployment goes smoothly i will
look at finding time to submit a PSS ticket to figure out why this happens.
 
Hi,

I'm sorry to hear that the problem still remains. Since you've repro it
through severals tests on your side. I do think that a thorough
troubleshooting or analisis in depth is needed. Also, I think that your
further test on another Win2k3 server you mentioned is also possible to
provide some clues on this. Anyway, Hope you'll soon firgure out the
problem. Thanks.

Regards,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)

Get Preview at ASP.NET whidbey
http://msdn.microsoft.com/asp.net/whidbey/default.aspx
 
Yep. The Project just went live today. So I will check sometime today or
tomorrow if the same happens. And if it does then I will look into opening a
PSS ticket.
 
The bug Appears to show up in Windows 2003 server also. I am going to try to
find some time popping some harmless Debug Code Over the weekend since it is
now on a live server. Will Open a PSS ticket if the results backup my
initial findings



values
 
Back
Top