Remove additional slashes from URL

我与影子孤独终老i 提交于 2020-01-03 17:14:49

问题


In ASP.Net it is posible to get same content from almost equal pages by URLs like localhost:9000/Index.aspx and localhost:9000//Index.aspx or even localhost:9000///Index.aspx

But it isn't looks good for me.

How can i remove this additional slashes before user go to some page and in what place?


回答1:


Use this :

url  = Regex.Replace(url  , @"/+", @"/");

it will support n times




回答2:


see

https://stackoverflow.com/a/19689870

https://msdn.microsoft.com/en-us/library/9hst1w91.aspx#Examples

You need to specify a base Uri and a relative path to get the canonized behavior.

Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Console.WriteLine(myUri.ToString());



回答3:


This solution is not that pretty but very easy

do
{
    url = url.Replace("//", "/");
}
while(url.Contains("//"));

This will work for many slashes in your url but the runtime is not that great.




回答4:


Remove them, for example:

 url = url.Replace("///", "/").Replace("//", "/");



回答5:


Regex.Replace("http://localhost:3000///////asdasd/as///asdasda///asdasdasd//", @"/+", @"/").Replace(":/", "://")



来源:https://stackoverflow.com/questions/30483947/remove-additional-slashes-from-url

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!