progmatically rewritemap rules at runtime

风流意气都作罢 提交于 2019-12-13 12:14:40

问题


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?


回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/1178461/progmatically-rewritemap-rules-at-runtime

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!