I have a base url http://some.com/url/that/does/something and a relative url this/is/a/specific/path.
Whenever I try to combine the two using new Uri(baseUrl, relativeUrl) some part of the base url gets cropped. The results with above example is
var baseUrl = new Uri("http://some.com/url/that/does/something")
var relativeUrl = "this/is/a/specific/path";
var Url = new Uri(baseUrl, relativeUrl);
// result is = http://some.com/url/that/does/this/is/a/specific/path
As you might have noticed the something disappeared.
How am I supposed to solve this?
I couldn't find any examples by Googling or searching here on Stackoverflow.com.
You were missing /
Here is Your Answer
var baseUrl = new Uri("http://some.com/url/that/does/something/");
var relativeUrl = "this/is/a/specific/path";
var Url = new Uri(baseUrl, relativeUrl);
Related
I have a base url https://dev.services.com/this/is/nothing/
and the path "max/status"
When I try to add "max/status" to the base url the part this/is/nothing/ is removed. This just happens some times not always.
I am doing this:
IRestClient _builder;
var baseUrl = "https://dev.services.com/this/is/nothing/";
var nextUrl = "max/status";
var fullUrl = new Uri(baseUrl, nextUrl);
var client = new _builder.Build(fullUrl);
the result should be https://dev.services.com/this/is/nothing/max/status
but now is: https://dev.services.com/max/status
so, obviously I am getting an error because https://dev.services.com/this/is/nothing/max/status does not exist.
any idea?
Your code sample would not compile in its current form. To achieve your desired result, you'd need to change it like this:
var baseUrl = new Uri("https://dev.services.com/this/is/nothing/");
var fullUrl = new Uri(baseUrl, "max/status");
My Main domain is http://172.27.88.111. I have a relative path to a image file like Media/myimg.jpg
I need to convert this path into a full URL with the help of REST API in asp.net.
I want the output for the image path in POSTMAN something like
http://172.17.88.111/Media/myimg.jpg
Using a LINQPad script as an example, here are 3 examples - but the use of the 'Uri' class is generally the best way to advocate:
void Main()
{
var baseUrlStr = #"http://172.27.88.111";
var relUrlStr = #"/Media/myimg.jpg";
// # 1
var baseUri = new Uri(baseUrlStr);
var relUri = new Uri(relUrlStr, UriKind.Relative);
var absUri = new Uri(baseUri, relUri);
Console.WriteLine(absUri);
// # 2
Console.WriteLine($"{baseUrlStr}{relUrlStr}");
// # 3
Console.WriteLine(baseUrlStr + relUrlStr);
}
This should give the following output:
http://172.27.88.111/Media/myimg.jpg
http://172.27.88.111/Media/myimg.jpg
http://172.27.88.111/Media/myimg.jpg
The strings are identical but when passed as a variable it is not valid?
What the hell is going on? Is it a language bug? I'm running this in C# .Net Core
var postUrl = "http://www.contoso.com";
var postUri = new Uri("http://www.contoso.com"); // works
var uri = new Uri(postUrl); // does not work
If you pulling your hair, then it because there is space after first opening quote in postUrl. Please remove that space & your bug will be begone.
Worked around the problem by using.
var postUrl = "http://www.contoso.com";
var uriBuilder = new UriBuilder(postUrl);
var uri = uriBuilder.Uri
Still wondering wtf?
Just the daily wtf of a programmer doing his job.
Using System.DirectoryServices, one can get the highestCommittedUSN this way:
using(DirectoryEntry entry = new DirectoryEntry("LDAP://servername:636/RootDSE"))
{
var usn = entry.Properties["highestCommittedUSN"].Value;
}
However, I need to get this information from a remote ADLDS using System.DirectoryServices.Protocols, which does not leverage ADSI. Following is a simplified code sample of what I'm attempting to do:
using(LdapConnection connection = GetWin32LdapConnection())
{
var filter = "(&(highestCommittedUSN=*))";
var searchRequest = new SearchRequest("RootDSE", filter, SearchScope.Subtree, "highestCommittedUSN");
var response = connection.SendRequest(searchRequest) as SearchResponse;
var usn = response.Entries[0].Attributes["highestCommittedUSN"][0];
}
Unfortunately this kicks back a "DirectoryOperationException: The distinguished name contains invalid syntax." At first I thought there might be something wrong in GetWin32LdapConnection() but that code is called in numerous other places to connect to the directory and never errors out.
Any ideas?
Thanks for the idea, Zilog. Apparently to connect to the RootDSE, you have to specify null for the root container. I also switched the filter to objectClass=* and the search scope to "base." Now it works!
using(LdapConnection connection = GetWin32LdapConnection())
{
var filter = "(&(objectClass=*))";
var searchRequest = new SearchRequest(null, filter, SearchScope.Base, "highestCommittedUSN");
var response = connection.SendRequest(searchRequest) as SearchResponse;
var usn = response.Entries[0].Attributes["highestcommittedusn"][0];
}
I hope this saves someone else some time in the future.
Update:
I added the missing two variables to the repo code blush Sincere appologies about that - I was rushing out to pick up the kids and missed it when i quickly reviewed the post.
When i call the .NET framework's MakeRelativeUri(...) method and the pass in a Uri which contains a file path .. and the file path has spaces in it .. then there's a great disturbance in the Force, as if millions of voices suddenly cried out in terror, and were suddenly silenced.
Here's my repo code. Appologies for this being an MSTest repo and not something a bit nicer like NUnit or XUnit.
[TestMethod]
public void SadPanda()
{
// Arrange.
var outputPath =
#"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\___sa.bundle.#.css";
var sourcePath =
#"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
var sourcePathJussy =
#"C:\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
// Added missing 2x vars *blush*
var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute);
var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute);
var relativePath = "../images/home-feature-bg.png";
var resolvedSourcePath = new Uri(sourceUri + relativePath, true);
// Act.
var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath);
// Assert.
Assert.IsTrue(resolvedOutput.Contains("XWing")); // :~(
}
Now if you look at the output it is something evil with the real path of the location, etc.
Now if we remove the SPACES from the paths, it now works :)
[TestMethod]
public void DoubleRaindbowUnicorns()
{
// Arrange.
var outputPath =
#"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\___sa.bundle.#.css";
var sourcePath =
#"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
var sourcePathJussy =
#"C:\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
outputPath = outputPath.Replace(" ", "-");
sourcePath = sourcePath.Replace(" ", "-");
// Added missing 2x vars *blush*
var sourceUri = new Uri(Path.GetDirectoryName(sourcePath) + "/", UriKind.Absolute);
var outputUri = new Uri(Path.GetDirectoryName(outputPath) + "/", UriKind.Absolute);
var relativePath = "../images/home-feature-bg.png";
var resolvedSourcePath = new Uri(sourceUri + relativePath, true);
// Act.
var resolvedOutput = outputUri.MakeRelativeUri(resolvedSourcePath);
// Assert.
Assert.IsFalse(resolvedOutput.Contains("XWing")); // Here yee! Woot, say I!
}
the output of this image resource is ../images/home-feature-bg.png (which by fluke is the same as it's source path) .. and not the really long evil string.
Yes / no ?
Update 2:
Renamed the subject (to better reflect the answer).
You need to use the System.IO.Path.Combine() function. What's happening here is you are using a URI class which cannot contain spaces, which would require the %20 if it were a path for HTML.
I think you are misunderstanding the purpose of the MakeRelativeUri() function. It is supposed to be called on one absolute URI taking in another in order to return the relative URI which represents how to arrive at the second resource in context of the first.
Example:
var cssFileUri = new Uri(#"C:\Temp\Spaces in this path\foo.css");
var imageFileUri = new Uri(#"C:\Temp\Images\bar.png");
var relativeUri = cssFileUri.MakeRelative(imageFileUri);
// The value of relativeUri is "../Images/bar.png"
So you can see how the relativeUri value gets us to the "bar.png" file from "foo.css".
In your example you'd pass in your CSS file path and a path to the image to get the value you have in relativePath.
In order to "undo" that (find the full path of an image in relation to a given CSS file) you construct a new Uri object:
var relativeImageUri = #"..\Image Folder\bar.png";
var cssFileUri = new Uri(#"C:\Some Path\Other Folder\foo.css");
var absoluteImageUri = new Uri(cssFileUri, relativeImageUri);
// The value of absoluteImageUri is "file:///C:/Some Path/Image Folder/bar.png"
So in order to find the resolvedOutput from your sample code:
var outputPath = #"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010 Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\___sa.bundle.#.css";
var sourcePath = #"C:\Users\AAAAAAA.BBBBB\Documents\Visual Studio 2010\Projects\XWing\Code\CCCCCCCCCCC.XWing.Application.Web\content\shared\css\home.css";
var relativePath = "../images/home-feature-bg.png";
var outputUriBase = new Uri(outputPath);
var resolvedOutput = new Uri(outputUriBase, relativePath);
You can now use the resolvedOutput Uri object to access the "home-feature-bg.png" image relative to the output path.
All these handle spaces in the path without error.
The
uriInstance.MakeRelativeUri()
method seems to be corrupt. The resulting Uri will show escaped characters when called with ToString(), e.g. %20 for an empty space. Other Uri instances don't do so, not even relative ones.
I wanted to use it for creating relative paths by subtracting the root path from a full path, then store a string from it in a DB
var baseUri = new Uri("http://myserver/pics")
var fullUri = new Uri("http://myserver/pics/myself/smile a.jpg")
var madeRelativeUri = baseUri.MakeRelativeUri(fullUri)
// madeRelativeUri.ToString() = "myself/smile%20a.jpg"
var manualRelativeUri = new Uri(#"myself/smile b", UriKind.Relative)
// manualRelativeUri.ToString() = "myself/smile b.jpg" // no escape