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?
Use this :
url = Regex.Replace(url , #"/+", #"/");
it will support n times
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());
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.
Remove them, for example:
url = url.Replace("///", "/").Replace("//", "/");
Regex.Replace("http://localhost:3000///////asdasd/as///asdasda///asdasdasd//", #"/+", #"/").Replace(":/", "://")
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:))//", #"/");
}
}
Related
guys i tried this one:
current: \netcoreapp2.1\ResultPath
target : \netcoreapp2.1\ReportPath\report1
AssemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
Uri currentPath = new Uri(AssemblyDirectory + "//ResultPath");
Uri targetPath = new Uri(reportsSubDir);
Uri relPath = currentPath.MakeRelativeUri(targetPath);
In result i'm getting
relPath.OriginalString = ReportPath/report1
Why is not ../ReportPath/report1 ?
And now for example:
if i have
current: \netcoreapp2.1\ResultPath\Test
target : \netcoreapp2.1\ReportPath\report1
I'm getting correct result in this way
relPath.OriginalString = ../../ReportPath/report10
Could anyone explain me why in first step i'm getting bad Relative Path, but in second good
and any idea how can i fix it, if i want to use first and second examples in my way ?
Well, if you are using .NET 5, you're in luck, since new method was included: Path.GetRelativePath(string, string)
If not, we can always dig into MS .NET Framework code:
https://referencesource.microsoft.com/#System.Workflow.ComponentModel/AuthoringOM/Design/DesignerHelpers.cs,a561eb779ce7163d
And copy implementation, which is:
static class PathHelper
{
internal static string GetRelativePath(string pathFrom, string pathTo)
{
Uri uri = new Uri(pathFrom);
string relativePath = Uri.UnescapeDataString(uri.MakeRelativeUri(new Uri(pathTo)).ToString());
relativePath = relativePath.Replace(Path.AltDirectorySeparatorChar, Path.DirectorySeparatorChar);
if (!relativePath.Contains(Path.DirectorySeparatorChar.ToString()))
relativePath = "." + Path.DirectorySeparatorChar + relativePath;
return relativePath;
}
}
Usage then would be:
var p1 = #"C:\users\turek\source\";
var p2 = #"C:\users\turek\desktop\";
PathHelper.GetRelativePath(p1, p2);
// returns "..\\desktop\\"
Found answer added / to current Path it looks like ::
Uri currentPath = new Uri(AssemblyDirectory + "//ResultPath/");
Still need explanation :)
This is my string
string link = "http%3A%2F%2Feurocommunicator.ge%2Fgeo%2Fview_myth%2F229"
I want to get absolute uri but it throws exception that uri is invalid :
Uri uri = new Uri(link);
What's wrong in this sting?
i want to get something like this :
http://eurocommunicator.ge/geo/view_myth/229
You can use :
string decodedUrl = Uri.UnescapeDataString(url)
https://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring(v=vs.110).aspx
Works like a charm in linqpad
void Main()
{
var url="http%3A%2F%2Feurocommunicator.ge%2Fgeo%2Fview_myth%2F229";
string decodedUrl = Uri.UnescapeDataString(url);
Console.WriteLine(decodedUrl);
}
You can try doing this:
Uri uri = new Uri("http://eurocommunicator.ge/geo/view_myth/229");
or
string link = "http%3A%2F%2Feurocommunicator.ge%2Fgeo%2Fview_myth%2F229"
Uri uri = new Uri(Server.UrlDecode(link));
Ok ok my fail HttpUtility.UrlDecode works fine, i was using HtmlDecode instead of it. How stupid am i
I am using Uri class for application development and needs the first segment of user-entered uri that either it contains http:// or http:// or ftp:// etc.
If not so,i have to hardcode to add to it.
I have already searched for it using googling and stackoverflowing but they didn't showed the precise requirement for me.
string path,downloadURL;
path = this.savePath.Text;
downloadURL = this.downloadURL.Text;
// i have done this but it didn't check if already existing .
downloadURL = "http://" + downloadURL;
Uri tmp = new Uri(downloadURL);
//extracts the last element
string EndPathFileName = tmp.Segments.Last();
// something like this but it returns only '/'.
//string StartPathFileName = tmp.Segments.First();
//Console.WriteLine(StartPathFileName);
Any suggestions?
Well there are a few options depending on what behavior you want...
You could just check if it contains :// which might be enough for what you want:
if(!downloadURL.Contains("://"))
downloadURL = "http://" + downloadURL;
Note that this would allow things such as "rubbish://www.example.com"
If you wanted to be a bit more cautious, you could check if the string starts with one of your predetermined values. For example:
if(!downloadURL.StartsWith("http://") && !downloadURL.StartsWith("https://") && !downloadURL.StartsWith("ftp://"))
downloadURL = "http://" + downloadURL;
Though this would mean that "rubbish://www.example.com" would become "http://rubbish://www.example.com".
You could go for a mix of both options, but keep in mind it can become very difficult to cope with all kinds of user input.
One final suggestion, which is even more robust, might be as follows:
string[] approvedSchemes = new string[] { "http", "https", "ftp" };
string userScheme = "";
if(downloadURL.Contains("://"))
{
// Get the first scheme defined, we will use this if it is in the approved list.
userScheme = downloadURL.Substring(0, downloadURL.IndexOf("://"));
// To cater for multiple :// remove all of them
downloadURL = downloadURL.Substring(downloadURL.LastIndexOf("://") + 3);
}
// Check if the user defined scheme is in the approved list, if not then set to http.
if(Array.IndexOf(approvedSchemes, userScheme.ToLowerInvariant()) > -1)
downloadURL = userScheme + "://" + downloadURL;
else
downloadURL = "http://" + downloadURL;
Here is a working example
You need to use Uri.Scheme Property
Uri baseUri = new Uri("http://www.contoso.com/");
Console.WriteLine(baseUri.Scheme); //http
Uri anotherUri = new Uri("https://www.contoso.com/");
Console.WriteLine(anotherUri.Scheme); //https
I have seen some topics about this already but those we're a bit unclear about what I want,
Im making a Special WebBrowser in C#
But, I want to split the TextBox text
Like:
Textbox1.Text = "http://google.com/lol";
I want,
Label1.Text = "http://google.com";
And
Label2.Text = "/lol";
But i want it to like detect the URL from TextBox1.Text not just google.com, every url
Like:
[
Label1.Text = Label1.Text("ignorefrom/");
Label2.Text = Label2.Text("ignorefromno/");
]
Ofcourse the ting above isnt possible, but thats basicly what I mean
Anny1 know how I can do that
Possible better explaining.
I'm making a web browser I want to detect the
URL: for example: http://google.com/lol
I want the first and second part from an url in a label
So: http://google.c om in label1 and /lol in label2 with every url
I have seen multiple topics about this but this was a bit different then my case
You should check out the documentation for the .NET class Uri. It has the functionality you're looking for.
Example:
var url = new Uri("http://www.google.com/some/path/file.aspx");
Console.WriteLine(url.Host); // prints www.google.com
Console.WriteLine(url.AbsolutePath); // prints /some/path/file.aspx
Properties used in this example:
Host
AbsolutePath
I would use the right tool, in this case you could use Uri.TryCreate:
string url = "http://google.com/lol";
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
Textbox1.Text = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
Label1.Text = uri.AbsolutePath;
}
If you want to include the query as mentioned in a comment you should use uri.PathAndQuery. Then "http://google.com/lol?lol=1" returns /lol?lol=1 as desired.
Thx Tim Schmelter
string url = "http://google.com/lol";
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
Textbox1.Text = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
Label1.Text = uri.AbsolutePath;
}
works like a charm :)
This should give the result you want:
using System.Uri;
Uri uri = new Uri("http://google.com/lol");
Label1.Text = uri.AbsoluteUri;
Label2.Text = uri.PathAndQuery;
Given an absolute URI/URL, I want to get a URI/URL which doesn't contain the leaf portion. For example: given http://foo.com/bar/baz.html, I should get http://foo.com/bar/.
The code which I could come up with seems a bit lengthy, so I'm wondering if there is a better way.
static string GetParentUriString(Uri uri)
{
StringBuilder parentName = new StringBuilder();
// Append the scheme: http, ftp etc.
parentName.Append(uri.Scheme);
// Appned the '://' after the http, ftp etc.
parentName.Append("://");
// Append the host name www.foo.com
parentName.Append(uri.Host);
// Append each segment except the last one. The last one is the
// leaf and we will ignore it.
for (int i = 0; i < uri.Segments.Length - 1; i++)
{
parentName.Append(uri.Segments[i]);
}
return parentName.ToString();
}
One would use the function something like this:
static void Main(string[] args)
{
Uri uri = new Uri("http://foo.com/bar/baz.html");
// Should return http://foo.com/bar/
string parentName = GetParentUriString(uri);
}
Thanks,
Rohit
Did you try this? Seems simple enough.
Uri parent = new Uri(uri, "..");
This is the shortest I can come up with:
static string GetParentUriString(Uri uri)
{
return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length);
}
If you want to use the Last() method, you will have to include System.Linq.
There must be an easier way to do this with the built in uri methods but here is my twist on #unknown (yahoo)'s suggestion.
In this version you don't need System.Linq and it also handles URIs with query strings:
private static string GetParentUriString(Uri uri)
{
return uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments[uri.Segments.Length -1].Length - uri.Query.Length);
}
Quick and dirty
int pos = uriString.LastIndexOf('/');
if (pos > 0) { uriString = uriString.Substring(0, pos); }
Shortest way I found:
static Uri GetParent(Uri uri) {
return new Uri(uri, Path.GetDirectoryName(uri.LocalPath) + "/");
}
PapyRef's answer is incorrect, UriPartial.Path includes the filename.
new Uri(uri, ".").ToString()
seems to be cleanest/simplest implementation of the function requested.
I read many answers here but didn't find one that I liked because they break in some cases.
So, I am using this:
public Uri GetParentUri(Uri uri) {
var withoutQuery = new Uri(uri.GetComponents(UriComponents.Scheme |
UriComponents.UserInfo |
UriComponents.Host |
UriComponents.Port |
UriComponents.Path, UriFormat.UriEscaped));
var trimmed = new Uri(withoutQuery.AbsoluteUri.TrimEnd('/'));
var result = new Uri(trimmed, ".");
return result;
}
Note: It removes the Query and the Fragment intentionally.
new Uri(uri.AbsoluteUri + "/../")
Get segmenation of url
url="http://localhost:9572/School/Common/Admin/Default.aspx"
Dim name() As String = HttpContext.Current.Request.Url.Segments
now simply using for loop or by index, get parent directory name
code = name(2).Remove(name(2).IndexOf("/"))
This returns me, "Common"
Thought I'd chime in; despite it being almost 10 years, with the advent of the cloud, getting the parent Uri is a fairly common (and IMO more valuable) scenario, so combining some of the answers here you would simply use (extended) Uri semantics:
public static Uri Parent(this Uri uri)
{
return new Uri(uri.AbsoluteUri.Remove(uri.AbsoluteUri.Length - uri.Segments.Last().Length - uri.Query.Length).TrimEnd('/'));
}
var source = new Uri("https://foo.azure.com/bar/source/baz.html?q=1");
var parent = source.Parent(); // https://foo.azure.com/bar/source
var folder = parent.Segments.Last(); // source
I can't say I've tested every scenario, so caution advised.