【问题标题】:Replace %20 in Url with - in C#.net web.config将 Url 中的 %20 替换为 C#.net web.config 中的 -
【发布时间】:2013-05-09 07:30:27
【问题描述】:

我正在尝试在 C#.net framework=4.0 和 Visual Studio 2010 版中重写 url,我在 web.config 文件中编写了以下代码行,但它不影响 url。我想用“-”替换 url 中的 %20

<rewrite>
  <rules>
    <rule name="Myrule">
      <match url="(.)\ (.)" />
      <action type="Rewrite" url="{R:0}-{R:1}" />
    </rule>
  </rules>
</rewrite> 

在 Chrome 中显示 %20,在 Mozilla 中显示 " " 空格,如何将空格替换为破折号 "-"?

【问题讨论】:

标签: c# asp.net-mvc url-rewriting


【解决方案1】:

更新:

我认为解决您的问题的唯一方法是编写您自己的重写模块。这些文章可能会有所帮助:

http://www.upperstrata.com/insights/2010-02-26/using-iis7-url-rewrite-module-with-asp-net-routing/

http://www.jphellemons.nl/post/URL-rewrite-with-aspnet-40-and-IIS7.aspx

我无法测试它,如果它以这种方式工作,我也不会,但用另一个正则表达式试试:

((%20)|\ )+

例如,像 http://www.goo%20le.de / hfdhdhdhd 这样的 Url 应该替换为 http://www.goo-le.de-/-hfdhdhdhd

更新:

它适用于正则表达式测试器http://regexpal.com/

【讨论】:

  • 嘿@Kai 你能告诉我什么是动作类型吗?
【解决方案2】:

也许可以尝试关注this tutorial 来尝试开发您自己的自定义重写提供程序。

【讨论】:

    【解决方案3】:

    试试这个,希望这会奏效。

    <%# Server.UrlDecode("www.stack%20overflow.com").Replace("%20", "-") %>
    

    【讨论】:

      猜你喜欢
      • 2011-10-08
      • 1970-01-01
      • 2014-06-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-19
      • 2016-05-12
      相关资源
      最近更新 更多