Is it possible to combine server variables to be passed to the IRewriteProvider Rewrite method? For example, if my custom provider is interested in ALL_HTTP & REMOTE_ADDR I only have the ability to access one when specifying the Rewrite method parameter
in config, like so:
{TestRewriteProvider:{ALL_HTTP}}
Is there a syntax for combining server variables or am I out of luck? Any other suggestions? If not, it would have been nice to have an overload of the Rewrite method accepting a string array. Could have been expressed in config via comma delimited variables,
like this:
{TestRewriteProvider:{ALL_HTTP,REMOTE_ADDR}}
Think I got it worked out by defining a new server variable combining the values I'm interested in, then passing that as the parameter to IRewriteProvider.Rewrite. One thing I noticed though... Do conditions fire prior to the definition of custom server
variables? If I called my custom provider in the rewrite action then I get the combined value. If I call it in a condition then I get nothing...
Better yet, looks like I can use the settings dictionary passed to Initialize... which I thought could only contain static configuration values. I believe I can specify server variables there as well.
Just to follow up in case anybody else is curious...
Initialize is only called once, not per request. So depending on what server variables your custom provider cares about you could do a few different things. For server specific variables that you are sure will not vary per request, they can be populated
via the provider's settings dictionary in config that gets passed to Initialize. However, for request specific variables, you would need to pass them to the Rewrite method, potentially combining them in a custom server variable if so desired.
Also, if you want the IRewriteProvider.Rewrite method to fire on every request regardless of whether the input value varies you'll need to set IRewriteContext.RewriteCacheEnabled to false (it is true by default). Probably not the best idea from a performance
standpoint, but ultimately just depends on how expensive the code within your Rewrite method is to execute.
-Mike
Marked as answer by Leo Tang - MSFT on Mar 17, 2010 04:05 AM
Thanks for trying this out and posting your comments. You are right about the Initialize method.
Also, as you probably already know, you cannot pass multiple parameters on the Rewrite method, so what you could do is combine them into a single string by using some separator and then parse that in the provider code, e.g.:
Mike Ayling
60 Posts
URL Rewrite 2 Extensibility - IRewriteProvider.Rewrite parameter
Mar 12, 2010 02:54 PM|LINK
Is it possible to combine server variables to be passed to the IRewriteProvider Rewrite method? For example, if my custom provider is interested in ALL_HTTP & REMOTE_ADDR I only have the ability to access one when specifying the Rewrite method parameter in config, like so:
{TestRewriteProvider:{ALL_HTTP}}
Is there a syntax for combining server variables or am I out of luck? Any other suggestions? If not, it would have been nice to have an overload of the Rewrite method accepting a string array. Could have been expressed in config via comma delimited variables, like this:
{TestRewriteProvider:{ALL_HTTP,REMOTE_ADDR}}
Thanks
-Mike
Mike Ayling
60 Posts
Re: URL Rewrite 2 Extensibility - IRewriteProvider.Rewrite parameter
Mar 12, 2010 03:08 PM|LINK
Think I got it worked out by defining a new server variable combining the values I'm interested in, then passing that as the parameter to IRewriteProvider.Rewrite. One thing I noticed though... Do conditions fire prior to the definition of custom server variables? If I called my custom provider in the rewrite action then I get the combined value. If I call it in a condition then I get nothing...
-Mike
Mike Ayling
60 Posts
Re: URL Rewrite 2 Extensibility - IRewriteProvider.Rewrite parameter
Mar 12, 2010 03:25 PM|LINK
Better yet, looks like I can use the settings dictionary passed to Initialize... which I thought could only contain static configuration values. I believe I can specify server variables there as well.
-Mike
Mike Ayling
60 Posts
Re: URL Rewrite 2 Extensibility - IRewriteProvider.Rewrite parameter
Mar 12, 2010 09:00 PM|LINK
Just to follow up in case anybody else is curious...
Initialize is only called once, not per request. So depending on what server variables your custom provider cares about you could do a few different things. For server specific variables that you are sure will not vary per request, they can be populated via the provider's settings dictionary in config that gets passed to Initialize. However, for request specific variables, you would need to pass them to the Rewrite method, potentially combining them in a custom server variable if so desired.
Also, if you want the IRewriteProvider.Rewrite method to fire on every request regardless of whether the input value varies you'll need to set IRewriteContext.RewriteCacheEnabled to false (it is true by default). Probably not the best idea from a performance standpoint, but ultimately just depends on how expensive the code within your Rewrite method is to execute.
-Mike
ruslany
900 Posts
Microsoft
Moderator
Re: URL Rewrite 2 Extensibility - IRewriteProvider.Rewrite parameter
Mar 12, 2010 10:33 PM|LINK
Hi Mike,
Thanks for trying this out and posting your comments. You are right about the Initialize method.
Also, as you probably already know, you cannot pass multiple parameters on the Rewrite method, so what you could do is combine them into a single string by using some separator and then parse that in the provider code, e.g.:
<conditions>
<add input="{TestRewriteProvider:{REMOTE_ADDR}#{HTTP_HOST}#{HTTP_REFERER}}" pattern=".+" />
</conditions>