8

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?

demo
  • 6,038
  • 19
  • 75
  • 149

6 Answers6

6

Use this :

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

it will support n times

enter image description here

Royi Namir
  • 144,742
  • 138
  • 468
  • 792
6

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());
Community
  • 1
  • 1
Bernhard
  • 2,541
  • 1
  • 26
  • 24
1

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.

Jens
  • 2,592
  • 2
  • 21
  • 41
  • 1
    This won't work if URL string contains something like this 'https://examplewebsite.com//some-page' it will remove '//' from 'https://' and URL wont work. – Rushabh Master Feb 18 '19 at 23:02
0

Remove them, for example:

 url = url.Replace("///", "/").Replace("//", "/");
L-Four
  • 13,345
  • 9
  • 65
  • 109
0

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

AuthorProxy
  • 7,946
  • 3
  • 27
  • 40
0

Here is the code snippets for combining URL segments, with the ability of removing the duplicate slashes:

    public class PathUtils
    {

        public static string UrlCombine(params string[] components)
        {
            var isUnixPath = Path.DirectorySeparatorChar == '/';

            
            for (var i = 1; i < components.Length; i++)
            {
                if (Path.IsPathRooted(components[i])) components[i] = components[i].TrimStart('/', '\\');
            }

            var url = Path.Combine(components);

            if (!isUnixPath)
            {
                url = url.Replace(Path.DirectorySeparatorChar, '/');
            }
            return Regex.Replace(url, @"(?<!(http:|https:))//", @"/");
        }
    }
LoremIpsum
  • 1,652
  • 18
  • 27