简体   繁体   中英

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.

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";
        }
    }
}

step 1:- download urlrewrite2.exe Here

Step 2:- write you logic in web.config

<system.webServer>
  <rewrite>
    <providers>
      <provider name="FileMap" type="FileMapProvider, Microsoft.Web.Iis.Rewrite.Providers, Version=7.1.761.0, Culture=neutral, PublicKeyToken=0545b0627da60a5f">
        <settings>
          <add key="FilePath" value="D:\j\branches\JuzBuzz\App_Data\rewriteurl.txt" />
          <add key="IgnoreCase" value="1" />
          <add key="Separator" value="," />
        </settings>
      </provider>
    </providers>
    <rules>
      <rule name="FileMapProviderTest" stopProcessing="true">
        <match url="(.*)" ignoreCase="false" />
        <conditions logicalGrouping="MatchAll">
          <add input="{FileMap:{R:1}}" pattern="(.+)" ignoreCase="false" />
        </conditions>
        <action type="Rewrite" url="{C:1}" appendQueryString="false" />
      </rule>
    </rules>
  </rewrite>
</system.webServer>

step 3:- Put you .txt file in App_Code folder or another place for which you have given the address in web.config , txt file will have data like

**technology,expert/search-expert.aspx?CatId=1

counselling-personal-growth,expert/search-expert.aspx?CatId=2** etc**

Microsoft has Microsoft.Web.Administration.dll available to help you out, but it requires administrator permissions to execute,

https://www.iis.net/learn/manage/scripting/how-to-use-microsoftwebadministration

It is quite suitable for a WinForms application (such as IIS Manager) to control an IIS server, but can also be used in other types of applications.

I do have a personal project that is a custom MWA implementation that works for some non-administrator cases. If you are interested in it, let me know.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM