views:

29

answers:

1

Hi, I am using the rather excellent IIS7 Rewrite module (V2), and want to create a custom RewriteProvider that rewrites differently depeneding on whether the physical file exists.

I have successfully created a provider, as in this tutorial: http://learn.iis.net/page.aspx/804/developing-a-custom-rewrite-provider-for-url-rewrite-module/

However, really need to be able to map the url to a physical path - I would normally do this via HttpContext.Current.Server.MapPath() or HostingEnvironment.MapPath(), but it looks like the HttpContext has not been initialised (at least within the current App Domain - since the ReWrite module is native code, I'm having difficulty working out where I can get this information).

I don't really want to have to resort to creating my own rewrite module to get around this problem - anyone have any clues for me?

Thanks! Mark.

+1  A: 

You will not be able to get to it using those APIs since the code runs in a different AppDomain than the one ASP.NET is using.

The only way I can think to make this work is to pass the right Server Variable that includes the physical path to your extension and do a Path.Combine() yourself.

So assuming you have an extension called YourProvider that you are calling somehow like this: {YourProvder:{URL}}

You can do: {YourProvder:{APPL_PHYSICAL_PATH}|{URL}}

You can now get the physical path and the URL separated by a pipe | , make sure to pass the Physical Path first since the URL is in the control of external users you do not want them to trick you into getting a different physical path.

CarlosAg
Thanks - I'd just spent half a day writing a DirectoryEntry version of MapPath() - your solution is much neater!One thing though, the APPL_PHYSICAL_PATH variable doesn't given an indication where in the URL it starts... but the PATH_TRANSLATED variable does indeed seem to map directly to the physical path in IIS 7.0
Mark

related questions