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;
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
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'm trying to get the absolute URI of each anchor tag on a Wikipedia page. I think the .href property should give the absolute URI but when I'm trying it in CsQuery I'm finding that it still gives me the relative URI. How should I get the absolute URI?
static void Main(string[] args)
{
string url = "https://en.wikipedia.org/wiki/Barack_Obama";
var dom = CQ.CreateFromUrl(url);
var selected = dom["div#mw-content-text a"];
foreach (var a in selected)
Console.WriteLine(a["href"]);
}
CsQuery shows you whatever exists in HTML page...
You can simply do that:
string domain = "https://en.wikipedia.org";
var dom = CQ.CreateFromUrl(url);
List<string> urls = new List<string>();
dom["a[href]"].Each(dom=>{
string url = dom.GetAttribute("href");
if(!url.StartsWith("https"))
url = domain + url;
urls.Add(url);
});
});
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?