views:

327

answers:

2

Is it possible to add static rewritemaps rules progmatically for ASP.NET 3.5

I have:-

<rewriteMaps>
    <rewriteMap name="My Name">
     <add key="/Sales" value="/Test.aspx?id=10" />
     <add key="/Sales-And-Marketing" value="/Test.aspx?id=10&amp;dog=cat" />
    </rewriteMap>
</rewriteMaps>

but would like to add these progmatically at runtime?

A: 

Basically, you could load some rules you create in your application from some persisted store (web.config file, database, etc...) at the application startup and then keep those rules stored in memory. I don't have the code for that, but I do have a code example for rewriting URLs at runtime from within ASP.NET. Check out this link for the code snippet:

http://www.upfromthesky.com/blog/post/2009/05/04/URL-Rewriting-in-ASPNET-via-HttpModule.aspx

You'll notice that I didn't have many rules so I just hard coded the rules within - but you could add something to load the rules (for example, as regular expressions) from memory to compare the current web url request to. Again, those could also be stored in a xml file so you can alter without having to recompile the code each time.

bbqchickenrobot
Rippo
Oh, i see... didn't catch that. Unfortunately, I haven't used that particular module. I have used others and found them to be a pain when needing to add rules programatically so I used the method I displayed above.
bbqchickenrobot
A: 

Its a little late I know, but you can do it programmatically by having the url in a seperate config

    <rewrite>
  <rewriteMaps  configSource="urls.config" />
  <rules>
    <rule name="Rewrite rule1 for StaticRedirect">
      <match url=".*" />
      <conditions>
        <add input="{StaticRedirect:{REQUEST_URI}}" pattern="(.+)" />
      </conditions>
      <action type="Rewrite" url="{C:1}" appendQueryString="false" />
    </rule>
  </rules>
</rewrite>

You then need to write to this config file when ever your urls change, I do it when there is a page altered and saved through the CMS admin system, using a webservice to talk to the client website and instruct it to rebuild the file.

The only issue I have is this doesnt seem to want work in Visual Studio Development Webserver

JamesStuddart