从代码 C# 更改 Web.Config 中的 URL 重写规则

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10752279/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 15:06:51  来源:igfitidea点击:

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

c#asp.netiisurl-rewritingweb-config

提问by levi

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

我想从 C# 代码修改重写规则。Url Rewrite 规则位于 web.config 文件中。

<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$" />,

我想换前任。<rule name="partners"> <match url="^partners$" /><rule name="partners"> <match url="^friendship/partners$" />

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

如何找到节点规则并将匹配 url 更新为“new one”,其中 name =“partners”;?

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

这是我对动态 url 重写的想法。如果你有任何其他方式,谢谢。

回答by Mehdi Bugnard

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

我使用以下代码更改了 web.config 网站中 connectionString 的值:

May be this example can help you (just change the value connectionStringby system.webServerand addby rulesetc.. Please tell me if it works for you

可能这个例子可以帮助你(只是改变值connectionString通过system.webServeradd通过rules等。请告诉我,如果你的作品

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

回答by Ankit

step 1:-download urlrewrite2.exe Here

第 1 步:-在此处下载 urlrewrite2.exe

Step 2:-write you logic in web.config

第 2 步:-在 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

第 3 步:-将您的 .txt 文件放在 App_Code 文件夹或您在 web.config 中提供地址的其他地方,txt 文件将包含类似的数据

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

**技术,专家/search-expert.aspx?CatId=1

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

咨询个人成长,专家/搜索专家.aspx?CatId=2** 等**

回答by Lex Li

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

Microsoft 有 Microsoft.Web.Administration.dll 可用于帮助您,但它需要管理员权限才能执行,

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

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.

非常适合WinForms应用程序(如IIS管理器)来控制IIS服务器,但也可以用于其他类型的应用程序。

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.

我确实有一个个人项目,它是一个自定义 MWA 实现,适用于一些非管理员案例。如果您对此感兴趣,请告诉我。