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
Related
Any way to get the current screen name of asp.net without hard coding?
string ScreenName = HttpContext.Current.Request.Url.AbsoluteUri;
I tried this and got the full url.
If you want to get the domain name from the url, use the following code:
string[] hostParts = new System.Uri(sURL).Host.Split('.');
string domain = String.Join(".", hostParts.Skip(Math.Max(0, hostParts.Length - 2)).Take(2));
or :
var host = new System.Uri(sURL).Host;
var domain = host.Substring(host.LastIndexOf('.', host.LastIndexOf('.') - 1) + 1);
where "sURL" is your URL.
I found a code. For me the string path is good
string url = HttpContext.Current.Request.Url.AbsoluteUri;
// http://localhost:1302/TESTERS/Default6.aspx
string path = HttpContext.Current.Request.Url.AbsolutePath;
// /TESTERS/Default6.aspx
string host = HttpContext.Current.Request.Url.Host;
// localhost
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/
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
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?:)/(?=[^/])", #"//");
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;