Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change URL Rewrite Rule in Web.Config from Code C#

I want to modify rewrite rule from C# code. Url Rewrite rule is resides in web.config file.

<system.webServer>
    <rewrite>
      <rules>
        <rule name="partners">
          <match url="^partners$" />
          <action type="Rewrite"
                  url="partners.aspx" />
        </rule>
        <rule name="news">
          <match url="^news$" />
          <action type="Rewrite"
                  url="news.aspx" />
        </rule>
        <rule name="projects">
          <match url="^projects$" />
          <action type="Rewrite"
                  url="projects.aspx" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>

I want to change for ex. <rule name="partners"> <match url="^partners$" /> to <rule name="partners"> <match url="^friendship/partners$" />,

how can I find node rule and update match url to "new one" where name = "partners";?

this is my idea for dynamic url rewriting. thanks for any other ways if you have.

like image 278
levi Avatar asked May 25 '12 09:05

levi


1 Answers

I change value for connectionString in my web.config website with this code :

May be this example can help you (just change the value connectionString by system.webServer and add by rules etc.. Please tell me if it works for you

XmlDocument myXmlDocument = new XmlDocument();
myXmlDocument.Load("../myPath/web.config");
foreach (XmlNode node in myXmlDocument["configuration"]["connectionStrings"])
{
    if (node.Name == "add")
    {
        if (node.Attributes.GetNamedItem("name").Value == "SCI2ConnectionString")
        {
            node.Attributes.GetNamedItem("connectionString").Value = "new value";
        }
    }
}
like image 74
Mehdi Bugnard Avatar answered Sep 27 '22 18:09

Mehdi Bugnard