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

后端 未结 3 1905
轮回少年
轮回少年 2021-01-02 08:49

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


    
               


        
相关标签:
3条回答
  • 2021-01-02 09:08

    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**

    0 讨论(0)
  • 2021-01-02 09:26

    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";
            }
        }
    }
    
    0 讨论(0)
  • 2021-01-02 09:30

    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.

    0 讨论(0)
提交回复
热议问题