问题
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. I want to replace the %20 in url with "-"
<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 "-"?
回答1:
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.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
Update:
It works in the regular expression tester http://regexpal.com/

回答2:
Maybe try following this tutorial to try developing your own custom rewrite provider.
回答3:
Try this out, Hope this will work.
<%# Server.UrlDecode("www.stack%20overflow.com").Replace("%20", "-") %>
来源:https://stackoverflow.com/questions/16456671/replace-20-in-url-with-in-c-net-web-config