从代码 C# 更改 Web.Config 中的 URL 重写规则 [英] Change URL Rewrite Rule in Web.Config from Code C#

查看:24
本文介绍了从代码 C# 更改 Web.Config 中的 URL 重写规则的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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>

我想换个ex.<代码><规则名称=合作伙伴"><match url="^partners$"/> 到 <match url="^friendship/partners$"/>,

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

如何找到节点规则并将匹配 url 更新为new one" where name = "partners";?

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

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

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

推荐答案

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

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

也许这个例子可以帮助你(只需改变 system.webServerconnectionString 值和 rulesadd代码>等.请告诉我它是否适合您

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

这篇关于从代码 C# 更改 Web.Config 中的 URL 重写规则的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆