I have a complete URL like: A: http://www.domain.com/aaa/bbb/ccc/ddd/eee.ext.
I have a relative URL like: B: ../../fff.ext
I’m looking for the easiest way in .NET C# to combine these two URLs and get:
C: http://www.domain.com/aaa/bbb/fff.ext
This is like what browsers does: you’re browsing URL A, then, page’s HTML have an hyperlink as B, the resulting URL is C.
You'd probably have better luck looking up "PathCanonicalize".
Also, with my findings, one of the overloaded Uri constructors can handle this:
Uri combined = new Uri(
new Uri("http://www.domain.com/aaa/bbb/ccc/ddd/eee.ext", UriKind.Absolute),
"../../fff.ext"
);
Proof is in the pudding
Related
I am made an API that works as it is intended, but to stop the direct use of the base URL of the API, there is another URL that masks the base URL but works the same.
I need to return the URL that is masking the base URL for pagination, but when I write
Request.Host I get the base URL, which should not be revealed.
A = https://baseurl.com/api
B = https://publicurl.com/api
B masks A
need to return B for pagination in the response.
Just need to get https://publicurl.com and not the whole url.
How can I get the URL(just the Host) of the one that is masking the base URL?
I add route to project.All work correct, but if i send normal url as first param its not correctly work.
Get["/{url}/{digit}"
If i send this params to server-all work correctly.
localhost:8888/google.com/2
But if i send param with http://www its not work.
localhost:8888/https://www.google.com/2
How correct pass url param to route? I think it because Nancy think that i send 3 input param.
If you really need to use GET instead of POST try HttpUtility.UrlEncode("https://google.com/2") to urlencode your url.
You have to encode your url which is send as a paramater:
Use:
var encodedString = Uri.EscapeDataString("https://www.google.com/2");
then your url will look like this and it shouldn't get any errors:
https%3A%2F%2Fwww.google.com%2F2
Sending the request:
localhost:8888/https%3A%2F%2Fwww.google.com%2F2
Or ou can use the
HttpUtility.UrlEncode();
method. For further information have a look at this.
Since you insist on changing the backend only, you could try using a regex to capture your route
Get["^(?<url>.*<digit>[0-9]+)$"]
This should match any url ending with atleast one number, and put everything before it in url like so:
Get["^(?<url>.*<digit>[0-9]+)$"] = parameters =>
{
var url = parameters.url;
var digit = parameters.digit;
};
I am currently unable to verify if this works as you want it to though, and to make sure you can adjust this yourself make sure to look into how to write regex
I have a ASP.NET MVC controller which is making call to another service using HttpClient class.
var url = "some url";
var client = new HttpClient();
var result= client.GetAsync(url);
The URL I am sending contains some special characters. How can encode special characters in ASP.NET MVC controller?
Thanks!!1
Try this:
url = HttpUtility.UrlEncode(url);
As you are considering a URL that will be used as such for a request (with HttpClient.getAsync) -- not as an argument within another URL -- you should use Uri.EscapeUriString.
Here is a comparison of three methods for the following URL:
var url = "http://some url?data=x y+z&user=1#ok";
HttpUtility.UrlEncode
Console.WriteLine(HttpUtility.UrlEncode(url));
http%3a%2f%2fsome+url%3fdata%3dx+y%2bz%26user%3d1%23ok
Obviously, this is not desired: the URL got damaged with / escaped, a + entered in the path, ...etc. The method seems useful for the query part of the URL, but not for the whole lot.
HttpUtility.UrlPathEncode
Console.WriteLine(HttpUtility.UrlPathEncode(url));
http://some%20url?data=x y+z&user=1#ok
This looks useful, although the space is a bit of a problem in the query part (notice the broken hyperlinking here, although browser can deal with it). But more importantly, the method is being deprecated:
Do not use; intended only for browser compatibility. Use UrlEncode.
Uri.EscapeUriString
Console.WriteLine(Uri.EscapeUriString(url));
http://some%20url?data=x%20y+z&user=1#ok
This seems to do the job well: %20 is an escape sequence that all modern browsers should support, also when occurring in the query part of the URL.
There is no need it encoding in Razor View Engine starting from 3rd version, and it's very convenient. Instead if you want to use tags you should use:
#Html.Raw(myString)
So basically just using Razor comes with encoding by default.
You should use HttpUtility.UrlPathEncode
When you use url = HttpUtility.UrlEncode(url) it doesn't work fine with spaces.
All,
In HTML, it is my understanding that a url that starts // (e.g. //www.google.com) refers to a protocol-less url that should be requested in the same scheme as that in which the page was served.
However, the following c# code fails
var uri = new Uri("//www.google.com", UriKind.RelativeOrAbsolute);
Assert.IsTrue(uri.IsAbsoluteUri);
Am I missing something here? At the moment I am rolling my own regex to find out if a URI is absolute:
return Regex.IsMatch(url, #"^(https?:)?//")
It's not absolute. It's relative to whether the URL is accessed from a source that is served over HTTP, HTTPS, or something else.
I would like to convert a URL like this: http://www.google.com to this google.com. So if the format of the URL is like this http://google.com it will be converted to this google.com
I need to determine if two urls are the same no matter the format of the URL. Is there a way to do this. I am using the Uri library in C# but the host value is different for http://www.google.com and http://google.com
You can use the Uri class to parse URIs.
Example: Get just the domain name from a URL?
You can extract more than the host name, too. Here is a full list of properties you can get from a Uri instance: http://msdn.microsoft.com/en-us/library/system.uri_properties(v=VS.71).aspx