HttpContext.RewritePath in .NET 2.0

J

James Higgs

For a long time, our product has had a "vanity URLs" feature where nice URLs
are mapped to ASPX files in an IHttpModule implementation, using HttpContext.RewritePath().
This has worked beautifully for the past couple of years under .NET 1.1,
but when it runs under ASP.NET 2.0 it has problems. Note that this is the
case regardless of whether the assmbly is built against 1.1 or 2.0

We're getting the following exception stack trace:

Object reference not set to an instance of an object.
at System.Web.HttpContext.RewritePath(VirtualPath filePath, VirtualPath
pathInfo, String queryString, Boolean setClientFilePath)
at System.Web.HttpContext.RewritePath(String filePath, String pathInfo,
String queryString)
at Interesource.Publish.Client.Web.VanityUrlModule.HttpApplication_AuthorizeRequest(Object
sender, EventArgs ea)

In my experience it's very unusual to get a NullReferenceException thrown
from inside the framework, so this warranted extra investigation.

The overload we're calling is HttpContext.RewritePath(string filePath, string
pathInfo, string queryString) and regardless of what values are passed to
the last two arguments, we get the NullReferenceException. I can't see from
Reflector how this method could possibly throw a NullReferenceException.

We did find a workaround, which was this to change our original line of:

ctx.RewritePath(physicalResource, queryString, pathInfo));

to this:

ctx.RewritePath(string.Concat(physicalResource, queryString.Length > 0 ?
"?" : "", queryString));

so that we were using a different overload on RewritePath which, according
to Reflector, doesn't go through the same code path as the first overload
we were using. As it happens, we don't need the path info. I'm at a loss
to see how this can be the result of something I'm doing wrong.

This workaround doesn't work conistently well, so at the moment, I have this
in my code:

if(Environment.Version.Major == 1)
{
ctx.RewritePath(physicalResource, req.PathInfo, queryString);
}
else
{
string url = physicalResource;
if(queryString != null && queryString.Length > 0)
{
url += "?" + queryString;
}
ctx.RewritePath(url);
}

Is it possible for this to be a bug in the framework? Is there some config
that I'm missing? Anyone else with the same issue?

I've logged the same issue at http://forums.asp.net/1109000/ShowPost.aspx
and blogged about it at http://staff.interesource.com/james/PermaLink.aspx?guid=1bd27f40-689a-41b9-b65a-4be68284949d

Hopefully this is a more appropriate place for a response.
 
J

Juan T. Llibre

The System.Web.HttpContext.RewritePath method, in 2.0, is :

System.Web.HttpContext.RewritePath(VirtualPath filePath,
VirtualPath pathInfo, String queryString, Boolean setClientFilePath)

You're getting the "Object reference not set to an instance of an object"
error because you're setting 3 parameters, while the method has 4.

I think that running :

ctx.RewritePath(physicalResource, queryString, pathInfo, false));

should fix your problem.

Use :

ctx.RewritePath(physicalResource, queryString, pathInfo, true));
if you want the clientFilePath parameter to be the same as the filePath parameter.

See : http://msdn2.microsoft.com/en-us/library/ms223300.aspx
 
J

James Higgs

Hello Juan,

I don't think that's the problem. The string, string, string overload has
been around since .NET 1.0 See here: http://tinyurl.com/bcz79

In any case, Reflector tells us that RewritePath(string, string, string)
calls RewritePath(string, string, string, bool) anyway
 
J

Juan T. Llibre

In that case, I'm fresh out of suggestions.

Last note :

The page you point me to : http://tinyurl.com/bcz79 , which maps to :
http://msdn.microsoft.com/library/d...ystemwebhttpcontextclassrewritepathtopic2.asp

states, very clearly, that
"This namespace, class, or member is supported only in version 1.1 of the .NET Framework"

Since you are running your app in 2.0, I'd take a close look at what that means.

The link I referred you to : http://msdn2.microsoft.com/en-us/library/ms223300.aspx
states that : "This method is new in the .NET Framework version 2.0"

Also, notice that the "optional" keyword is not used for that method.
It looks like all parameters are mandatory, including Boolean.

To me, it would seem that, even though "the string, string, string overload has been
around since .NET 1.0", the method works differently in 2.0 than it does in 1.0 or 1.1.

Did you try using ctx.RewritePath(physicalResource, queryString, pathInfo, false));

?
 
J

James Higgs

You're right that that overload was new in v1.1, but it is still supported
in .NET 2.0 - see here: http://msdn2.microsoft.com/en-us/library/7hkaxdb4(en-US,VS.80).aspx

To be absolutely clear, these calls do _not_ work:

ctx.RewritePath("/Default.aspx", "id=5", "", false);
ctx.RewritePath("/Default.aspx", "id=5", "", true);
ctx.RewritePath("/Default.aspx", "id=5", "");
ctx.RewritePath("/Default.aspx", null, null, false);
ctx.RewritePath("/Default.aspx", null, null, true);
ctx.RewritePath("/Default.aspx", null, null);

These calls do work:

ctx.RewritePath("/Default.aspx?id=5", false);
ctx.RewritePath("/Default.aspx?id=5", true);
ctx.RewritePath("/Default.aspx?id=5");
ctx.RewritePath("/Default.aspx");
ctx.RewritePath("/Default.aspx", false);
ctx.RewritePath("/Default.aspx", true);

This makes sense, because according to Reflector, the second lot of calls
are all processed in RewritePath(string, bool), whereas the other lot all
get processed in an internal method with the following signature:

internal void RewritePath(VirtualPath filePath, VirtualPath pathInfo, string
queryString, bool setClientFilePath);

This explains why one lot of overloads works and the other lot doesn't.

I should point out that this worked correctly on v1.1 and now doesn't on
v2.0 - it looks like a regression to me.

James
 
S

Steven Cheng[MSFT]

Hi James,

Thanks for your posting, also Juan's inputs.

As for the problem you mentioned, I'd like to comment something:
1.For ASP.NET 1.1 application, it can running correctly on target machine
with .net 2.0 framework through side by side execution as long as the 1.1
framework also installed..... Nothing in 2.0 framework will impact it.

2.It problem related to 2.0 framework if we migrate and recompile the web
application through .net 2.0 framework...(e.g by using VS.NET 2005). So
this should be your case , yes?

As for the

====================
ctx.RewritePath("/Default.aspx", "id=5", "", false);
ctx.RewritePath("/Default.aspx", "id=5", "", true);
ctx.RewritePath("/Default.aspx", "id=5", "");
ctx.RewritePath("/Default.aspx", null, null, false);
ctx.RewritePath("/Default.aspx", null, null, true);
ctx.RewritePath("/Default.aspx", null, null);
========================

I've also performed some tests on them in my local environemnt (
WIN2K3/IIS6 .NET 2.0, VS.NET 2005 PRO) , seems the above method call works
correctly in my ASP.NET 2.0 application( I've tried in both IIS and file
system app...). So I'm also wondering whether this is a project or
environment specific problem. Would you also also try testing on some
other machine to see whether this is the consistency behavior?

BTW, as for the below statement:

"This namespace, class, or member is supported only in version 1.1 of the
.NET Framework"

I think it is mainly target the differecne between 1.0 and 1.1 framework
(when 1.1 was the newly released one....), so this should not make sense to
our issue....

Thanks,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Message-ID: <[email protected]>
| From: James Higgs <[email protected]>
| Subject: Re: HttpContext.RewritePath in .NET 2.0
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Transfer-Encoding: 8bit
| Content-Type: text/plain; charset=iso-8859-1; format=flowed
| X-Newsreader: JetBrains Omea Reader 671.6
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Date: Mon, 14 Nov 2005 08:07:08 -0800
| NNTP-Posting-Host: ir-fw.interesource.com 217.169.43.2
| Lines: 1
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:357971
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Done, at
http://lab.msdn.microsoft.com/productfeedback/viewfeedback.aspx?feedbackid=a
4ed900e-debe-4733-9c1d-2b72a6b6efd9

|
|
 
J

James Higgs

Steven,

Thanks for your response. It's certainly possible that this is to do with project-specific issues, although we have the same problem on two separate projects, one upgraded from a VS2003 project and one a newly created one. Incidentally, these projects were created on different machines, so I don't think it's a machine-specific issue. It may be worth noting that both machines previously had Beta 2 installed, and one of them also subsequently had the Release Candidate installed. In both cases, the unistall procedure was followed correctly and VS2005 RTM installed without issues.

I attach a zip file with a small VS2005 web project that reproduces the problem on my machine. This repros the problem on two machines. The project is a vanilla project created using VS2005 by using File > New Web Site...

A v1.1 app running on a machine with v2.0 installed works correctly (I assume because it is running on v1.1 so 2.0 doesn't come into the equation).

Please note that I've logged this at the product feedback centre (http://lab.msdn.microsoft.com/Produ...edbackId=a4ed900e-debe-4733-9c1d-2b72a6b6efd9)

James
 
J

James Higgs

It seems like the ZIP file was removed in the upload of my post. In any case,
you can find the ZIP faile at the product feedback centre URL.
 
S

Steven Cheng[MSFT]

Hi James,

I've downloaded your repro project and try testing on my local environment.
Unfortunately, seems I can not reproduce the "Null Reference..." exception
you encountered.....

Also, here is something I've adjusted on my side after first time run you
code:
=========
void app_AuthorizeRequest(object sender, EventArgs e)
{
HttpApplication app = (HttpApplication)sender;
app.Context.RewritePath("/Default.aspx", "id=5", "");
}
=========


1. When specify the path for our web application in asp.net server code, we
should use the "~/..." path to specify Application Root, "/..." means
start from the website (IIS) root. So I think we should change the rewrite
code to

app.Context.RewritePath("~/Default.aspx"......);

2. As for the HttpContext.RewritePath(string, string ,string) function, the
querystring should be the third parameter rather than the second, not sure
whether its a type mistake, I changed it to

app.Context.RewritePath("/Default.aspx", "", "id=5");

and still works well.

Thanks,

Steven Cheng
Microsoft Online Support

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






--------------------
| Message-ID: <[email protected]>
| From: James Higgs <[email protected]>
| Subject: Re: HttpContext.RewritePath in .NET 2.0
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Transfer-Encoding: 8bit
| Content-Type: text/plain; charset=iso-8859-1; format=flowed
| X-Newsreader: JetBrains Omea Reader 671.6
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Date: Tue, 15 Nov 2005 01:27:52 -0800
| NNTP-Posting-Host: ir-fw.interesource.com 217.169.43.2
| Lines: 1
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP14.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:358167
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| It seems like the ZIP file was removed in the upload of my post. In any
case,
| you can find the ZIP faile at the product feedback centre URL.

|
|
 
J

James Higgs

Steven,

You're right about the typo with the id=5 - I've swapped these over and place
the tilde at the start of the path and this still makes no difference.

I can also repro it on another machine that doesn't have VS2005 installed
on it, but just IIS6, .NET 1.1 and .NET 2.0

James
 
S

Steven Cheng[MSFT]

Thanks for your followup James,

Yes, seems really strange. What make it difficult is that I'm wondering
whether the dev or test guys can also reproduce the behavior in their
environment since it may be hard to got a reproduce enviornment...

Thanks,

Steven Cheng
Microsoft Online Support

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


--------------------
| Message-ID: <[email protected]>
| From: James Higgs <[email protected]>
| Subject: Re: HttpContext.RewritePath in .NET 2.0
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Transfer-Encoding: 8bit
| Content-Type: text/plain; charset=iso-8859-1; format=flowed
| X-Newsreader: JetBrains Omea Reader 671.6
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Date: Wed, 16 Nov 2005 06:10:48 -0800
| NNTP-Posting-Host: ir-fw.interesource.com 217.169.43.2
| Lines: 1
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP12.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:358562
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Steven,
|
| You're right about the typo with the id=5 - I've swapped these over and
place
| the tilde at the start of the path and this still makes no difference.
|
| I can also repro it on another machine that doesn't have VS2005 installed
| on it, but just IIS6, .NET 1.1 and .NET 2.0
|
| James

|
|
 
J

James Higgs

Steven,

If it would help I might be able to repro this on a VPC image which you could
then use to diagnose the issue. Would that be useful?

Jame
 
J

James Higgs

Hmm. I tried this on a Windows 2003 Server /.NET 1.1 /.NET 2.0 VPC image,
and I can't reproduce the issue. Nevertheless, I've got three other machines
I can repro it on.
 
S

Steven Cheng[MSFT]

Thanks for your followup James,

While due to the policy limitation of newsgroup , we are not allowed to
accept VPC or large code project from external source for troubleshooting.
However, since this is likely a potential issue of the framework on certain
Server environment, I'd recommend you try contacting PSS and opening a
regular CASE on this, the support engineer there will help you do thorough
throubleshooting on this. Also, if this end to be a issue of the .net
framework, it's free of charge and will help provide you hotfix according
to the problem.

Thanks for your understanding,

Steven Cheng
Microsoft Online Support

Get Secure! www.microsoft.com/security
(This posting is provided "AS IS", with no warranties, and confers no
rights.)
--------------------
| Message-ID: <[email protected]>
| From: James Higgs <[email protected]>
| Subject: Re: HttpContext.RewritePath in .NET 2.0
| References: <[email protected]>
| MIME-Version: 1.0
| Content-Transfer-Encoding: 8bit
| Content-Type: text/plain; charset=iso-8859-1; format=flowed
| X-Newsreader: JetBrains Omea Reader 671.6
| Newsgroups: microsoft.public.dotnet.framework.aspnet
| Date: Thu, 17 Nov 2005 07:19:25 -0800
| NNTP-Posting-Host: ir-fw.interesource.com 217.169.43.2
| Lines: 1
| Path: TK2MSFTNGXA02.phx.gbl!TK2MSFTNGP08.phx.gbl!TK2MSFTNGP09.phx.gbl
| Xref: TK2MSFTNGXA02.phx.gbl
microsoft.public.dotnet.framework.aspnet:358923
| X-Tomcat-NG: microsoft.public.dotnet.framework.aspnet
|
| Hmm. I tried this on a Windows 2003 Server /.NET 1.1 /.NET 2.0 VPC image,
| and I can't reproduce the issue. Nevertheless, I've got three other
machines
| I can repro it on.

|
|
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,139
Latest member
JamaalCald
Top