I have a URL like this:
http://www.example.com/Catalog/Category/Detail
http://www.example.com/Catalog/Products/12
Now, I want to extract the /Catalog/Category/Detail and /Catalog/Products/12 part so I can append with some other base url. How can I do that easily?
Use Uri class, and use Uri.LocalPath property like:
Uri uri = new Uri("http://www.example.com/Catalog/Category/Detail");
Console.WriteLine(uri.LocalPath); // /Catalog/Category/Detail
var segments = new Uri("http://www.example.com/Catalog/Category/Detail").Segments;
This will return
/
Catalog/
Category/
Detail
Related
I've just started using .netcore.
I need to create a url, I've come across UriBuilder however I can only see example of using that with named paramters e.g.: localhost?value=abc&another=def, however I want to construct without parameter names, so:
localhost/abc/def/
Is this possible, I was thinking maybe something like the below:
var request = new HttpRequestMessage();
request.RequestUri = new Uri(_myUrl);
request.Method = HttpMethod.Get;
var uriBuilder = new UriBuilder(request.RequestUri);
var query = HttpUtility.ParseQueryString(uriBuilder.Query);
query[0] = "abc"
Any help appreciated.
That's not how UriBuilder or URIs work. What you are trying to change/set is the path of the URI. That is done using the Path property, or using the relevant constructor of UriBuilder. Your example shows how to change/set the Query-String (key-value pairs, basically).
How you create the path is not the business of the UriBuilder class (see #Irdis comment about an option).
Basically, you can create your path manually using string manipulation of your choice, then assign it to the Path property, e.g.:
var b = new UriBuilder(...);
// Set other properties to your requirements
b.Path = "/the/path/ optional with spaces and other stuff/";
You can then use the UriBuilder.Uri property to get the full URI or use the UriBuilder.ToString() method to get the properly escaped URI. For the above example that would be something like:
http://localhost/the/path/%20optional%20with%20spaces%20and%20other%20stuff/
Uri url = new Uri("http://www.website.com/content/a/?filter=porn");
Is there any way only get a string with "/content/a/" from the Uri?
I mean no domain or query string parameters without having to work with strings?
Not sure why #AlexK deleted his answer, but url.AbsolutePath will give you that info.
Uri url = new Uri("http://www.website.com/content/a/?filter=porn");
Console.WriteLine(url.AbsolutePath);
// outputs /content/a
https://dotnetfiddle.net/3koJ7v
I asked a question to get URL as action input here. Now I have a new problem. The passed URL to action changes from http://example.com to http:/example.com.
I want to know why and how can I resolve the problem.
P.S: I added this code to resolve but I think there may be another problems in future! the code is:
if ((url.Contains(":/")) && !(url.Contains("://")))
{
url = url.Replace(":/", "://");
}
The browser (or server) is replacing a double slash (illegal) with a single one.
Try it,
http://stackoverflow.com/questions/11853025//input-url-like-http-site-com-changes-to-http-site-com-in-action-input
(in Chrome) goes to:
http://stackoverflow.com/questions/11853025/input-url-like-http-site-com-changes-to-http-site-com-in-action-input
If I were you, I would remove the http:// from your path and add it later.
http://localhost:1619/Utility/PR/example.com/
Then, url = "http://" + url;
If you might get secure urls, add that to the route /http/example.com or /https/example.com
use regex:
string src = #"http://example.com";
string result = Regex.Replace(src, #"(?<=https?:/)/", "");
if you need to revert:
string src = #"http:/example.com";
string result = Regex.Replace(src, #"(?<=https?:)/(?=[^/])", #"//");
I have an SPListItem that I return a full image url:
http://sharepointsite.com/images/bob's picture.jpg
I return this url by calling this:
splistitem.File.ServerRelativeUrl
I want to be able to turn this url into this:
http://sharepointsite.com/images/bob%27s%20picture.jpg
but if I encode the full URL it will replace the / which i dont want. I want to be able to just get the ending image file and UrlEncode that, how would I go about solving this progmmatically?
Try this:
var url = "http://sharepointsite.com/images/bob's picture.jpg";
var basePath = System.IO.Path.GetDirectoryName(url);
var fileName = System.IO.Path.GetFileName(url);
var finalPath = basePath + "\\" + Uri.EscapeDataString(fileName);
You can achieve this by simply calling splistitem.File[SPBuiltInFieldId.EncodedAbsUrl] - it won't replace the forward-slashes. And then you can pass it to System.IO.Path.GetFileName if you want only the filename.
By the way, shouldn't ServerRelativeUrl return /images/bob's picture.jpg?
For this line of code;
string link = HttpContext.Current.Server.MapPath("/Contract/Details/" + this.ContractId.ToString());
I get the physical pathname on C drive.
What I want is the url, ie
http://localhost:1234/Contract/Details/1
How do I get this?
// Use the Uri constructor to form a URL relative to the current page
Uri linkUri = new Uri(HttpContext.Current.Request.Url, "/Contract/Details/" + this.ContractId.ToString());
string link = linkUri.ToString();
try this:
string url = HttpContext.Current.Request.Url.AbsoluteUri;
There's a great article on .Net paths # http://west-wind.com/weblog/posts/132081.aspx
Take a look at the Url or PathInfo property.
Uri base = new Uri("http://localhost:1234/";);
Uri file = new Uri(host, "/Contract/Details/" + this.ContractId.ToString());
string URL = file.AbsoluteUri;