简体   繁体   English

用C#.net web.config中的-替换Url中的%20

[英]Replace %20 in Url with - in C#.net web.config

I am trying to rewrite the url in C#.net framework=4.0 and visual studio version 2010, I have written following line of code in web.config file, but its not affecting the url. 我正在尝试在C#.net framework = 4.0和Visual Studio版本2010中重写url,我在web.config文件中编写了以下代码行,但它不影响url。 I want to replace the %20 in url with "-" 我想用“-”替换网址中的%20

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

In Chrome it shows %20 and in Mozilla it shows " " space, how do I replace the space with dash "-"? 在Chrome中,它显示%20,在Mozilla中,它显示“”空格,如何用破折号“-”替换该空格?

也许尝试按照本教程尝试开发自己的自定义重写提供程序。

UPDATE: 更新:

I think the only solution for your problem is to write your own Rewrite Module. 我认为解决您问题的唯一方法是编写自己的重写模块。 These article might help: 这些文章可能会有所帮助:

http://www.upperstrata.com/insights/2010-02-26/using-iis7-url-rewrite-module-with-asp-net-routing/ 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 http://www.jphellemons.nl/post/URL-rewrite-with-aspnet-40-and-IIS7.aspx

I cannot test it and I'm not if it works that way, but try it with another regular expression: 我无法对其进行测试,但我无法对其进行测试,但可以使用另一个正则表达式进行尝试:

((%20)|\ )+

For example an Url like this http://www.goo%20le.de / hfdhdhdhd should be replaced to this http://www.goo-le.de-/-hfdhdhdhd 例如,应将http://www.goo%20le.de / hfdhdhdhd这样的http://www.goo-le.de-/-hfdhdhdhd替换为http://www.goo-le.de-/-hfdhdhdhd

Update: 更新:

It works in the regular expression tester http://regexpal.com/ 它可以在正则表达式测试器http://regexpal.com/中运行

在此处输入图片说明

试试看,希望这会起作用。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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