This is my string
string link = "http%3A%2F%2Feurocommunicator.ge%2Fgeo%2Fview_myth%2F229"
I want to get absolute uri but it throws exception that uri is invalid :
Uri uri = new Uri(link);
What's wrong in this sting?
i want to get something like this :
http://eurocommunicator.ge/geo/view_myth/229
You can use :
string decodedUrl = Uri.UnescapeDataString(url)
https://msdn.microsoft.com/en-us/library/system.uri.unescapedatastring(v=vs.110).aspx
Works like a charm in linqpad
void Main()
{
var url="http%3A%2F%2Feurocommunicator.ge%2Fgeo%2Fview_myth%2F229";
string decodedUrl = Uri.UnescapeDataString(url);
Console.WriteLine(decodedUrl);
}
You can try doing this:
Uri uri = new Uri("http://eurocommunicator.ge/geo/view_myth/229");
or
string link = "http%3A%2F%2Feurocommunicator.ge%2Fgeo%2Fview_myth%2F229"
Uri uri = new Uri(Server.UrlDecode(link));
Ok ok my fail HttpUtility.UrlDecode works fine, i was using HtmlDecode instead of it. How stupid am i
Related
User input the url string in a textbox and I need to add a string "cmd" if not available .
Please suggest how to achieve this,
string cmdUrl = AddPrefix("https://google.com");
static string AddPrefix(string inputUrl)
{
return formattedUrl;// want to return https://cmd.google.com if the string cmd not added already in url
}
First, you can parse your url using var parsedUrl = new Uri(inputUrl).
Then check if the host includes your substring like this: parsedUrl.Host.StartsWith("cmd")
If it does, return your original url else build your new one: $"{parsedUrl.Scheme}://cmd.{parsedUrl.Host}{parsedUrl.PathAndQuery}"
In ASP.Net it is posible to get same content from almost equal pages by URLs like
localhost:9000/Index.aspx and localhost:9000//Index.aspx or even localhost:9000///Index.aspx
But it isn't looks good for me.
How can i remove this additional slashes before user go to some page and in what place?
Use this :
url = Regex.Replace(url , #"/+", #"/");
it will support n times
see
https://stackoverflow.com/a/19689870
https://msdn.microsoft.com/en-us/library/9hst1w91.aspx#Examples
You need to specify a base Uri and a relative path to get the canonized behavior.
Uri baseUri = new Uri("http://www.contoso.com");
Uri myUri = new Uri(baseUri, "catalog/shownew.htm");
Console.WriteLine(myUri.ToString());
This solution is not that pretty but very easy
do
{
url = url.Replace("//", "/");
}
while(url.Contains("//"));
This will work for many slashes in your url but the runtime is not that great.
Remove them, for example:
url = url.Replace("///", "/").Replace("//", "/");
Regex.Replace("http://localhost:3000///////asdasd/as///asdasda///asdasdasd//", #"/+", #"/").Replace(":/", "://")
Here is the code snippets for combining URL segments, with the ability of removing the duplicate slashes:
public class PathUtils
{
public static string UrlCombine(params string[] components)
{
var isUnixPath = Path.DirectorySeparatorChar == '/';
for (var i = 1; i < components.Length; i++)
{
if (Path.IsPathRooted(components[i])) components[i] = components[i].TrimStart('/', '\\');
}
var url = Path.Combine(components);
if (!isUnixPath)
{
url = url.Replace(Path.DirectorySeparatorChar, '/');
}
return Regex.Replace(url, #"(?<!(http:|https:))//", #"/");
}
}
I have seen some topics about this already but those we're a bit unclear about what I want,
Im making a Special WebBrowser in C#
But, I want to split the TextBox text
Like:
Textbox1.Text = "http://google.com/lol";
I want,
Label1.Text = "http://google.com";
And
Label2.Text = "/lol";
But i want it to like detect the URL from TextBox1.Text not just google.com, every url
Like:
[
Label1.Text = Label1.Text("ignorefrom/");
Label2.Text = Label2.Text("ignorefromno/");
]
Ofcourse the ting above isnt possible, but thats basicly what I mean
Anny1 know how I can do that
Possible better explaining.
I'm making a web browser I want to detect the
URL: for example: http://google.com/lol
I want the first and second part from an url in a label
So: http://google.c om in label1 and /lol in label2 with every url
I have seen multiple topics about this but this was a bit different then my case
You should check out the documentation for the .NET class Uri. It has the functionality you're looking for.
Example:
var url = new Uri("http://www.google.com/some/path/file.aspx");
Console.WriteLine(url.Host); // prints www.google.com
Console.WriteLine(url.AbsolutePath); // prints /some/path/file.aspx
Properties used in this example:
Host
AbsolutePath
I would use the right tool, in this case you could use Uri.TryCreate:
string url = "http://google.com/lol";
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
Textbox1.Text = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
Label1.Text = uri.AbsolutePath;
}
If you want to include the query as mentioned in a comment you should use uri.PathAndQuery. Then "http://google.com/lol?lol=1" returns /lol?lol=1 as desired.
Thx Tim Schmelter
string url = "http://google.com/lol";
Uri uri;
if (Uri.TryCreate(url, UriKind.Absolute, out uri))
{
Textbox1.Text = uri.Scheme + Uri.SchemeDelimiter + uri.Host;
Label1.Text = uri.AbsolutePath;
}
works like a charm :)
This should give the result you want:
using System.Uri;
Uri uri = new Uri("http://google.com/lol");
Label1.Text = uri.AbsoluteUri;
Label2.Text = uri.PathAndQuery;
Whats the equivalent to System.IO.Path ?
I got this url: http://www.website.com/category1/category2/file.aspx?data=123
How can i break this down, like
var url = ASPNETPATH("http://www.website.com/category1/category2/file.aspx?data=123");
url.domain <-- this would then return http://www.website.com
url.folder <-- would return category1/category2
url.file <-- would return file.aspx
url.queryString <-- would return the querystring in some format
Use the UriBuilder class:
http://msdn.microsoft.com/en-us/library/system.uribuilder.aspx
UriBuilder uriBuilder = new UriBuilder("http://www.somesite.com/requests/somepage.aspx?i=123");
string host = uriBuilder.Host; // www.somesite.com
string query = uriBuilder.Query; // ?i=123
string path = uriBuilder.Path; // /requests/somepage.aspx
Check out the URI Class you can get all of that information using that class.
Regular expressions are perfect to use for this.
Here's a link that has some nice info on using them.
http://www.regular-expressions.info/
Edit : I forgot C# has a URI class which you can use for this as well.
I have the following code snippet:
string tmp = String.Format("<SCRIPT FOR='window' EVENT='onload' LANGUAGE='JavaScript'>javascript:window.open('{0}');</SCRIPT>", url);
ClientScript.RegisterClientScriptBlock(this.GetType(), "NewWindow", tmp);
The URL generated by this code include the port number and I think that is happening because port 80 is used by the website and in this code I am trying to load a page from a virtual directory of the website. Any ideas on how to suppress the port number in the URL string generated by this code?
Use the Uri.GetComponents method. To remove the port component you'll have to combine all the other components, something like:
var uri = new Uri( "http://www.example.com:80/dir/?query=test" );
var clean = uri.GetComponents( UriComponents.Scheme |
UriComponents.Host |
UriComponents.PathAndQuery,
UriFormat.UriEscaped );
EDIT: I've found a better way:
var clean = uri.GetComponents( UriComponents.AbsoluteUri & ~UriComponents.Port,
UriFormat.UriEscaped );
UriComponents.AbsoluteUri preservers all the components, so & ~UriComponents.Port will only exclude the port.
UriBuilder u1 = new UriBuilder( "http://www.example.com:80/dir/?query=test" );
u1.Port = -1;
string clean = u1.Uri.ToString();
Setting the Port property to -1 on UriBuilder will remove any explicit port and implicitly use the default port value for the protocol scheme.
A more generic solution (works with http, https, ftp...) based on Ian Flynn idea.
This method does not remove custom port, if any.
Custom port is defined automatically depending on the protocol.
var uriBuilder = new UriBuilder("http://www.google.fr/");
if (uriBuilder.Uri.IsDefaultPort)
{
uriBuilder.Port = -1;
}
return uriBuilder.Uri.AbsoluteUri;
April 2021 update
With newer .NET versions, Uri.AbsoluteUri removes the default ports and retains the custom port by default. The above code-snippet is equivalent to:
var uriBuilder = new UriBuilder("http://www.google.fr/");
return uriBuilder.Uri.AbsoluteUri;
I would use the System.Uri for this. I have not tried, but it seems it's ToString will actually output what you want:
var url = new Uri("http://google.com:80/asd?qwe=asdff");
var cleanUrl = url.ToString();
If not, you can combine the components of the url-members to create your cleanUrl string.
var url = "http://google.com:80/asd?qwe=zxc#asd";
var regex = new Regex(#":\d+");
var cleanUrl = regex.Replace(url, "");
the solution with System.Uri is also possible but will be more bloated.
You can use the UriBuilder and set the value of the port to -1
and the code will be like this:
Uri tmpUri = new Uri("http://LocalHost:443/Account/Index");
UriBuilder builder = new UriBuilder(tmpUri);
builder.Port = -1;
Uri newUri = builder.Uri;
You can also use the properties of URIBuilder for this, it has properties for outputting an url the way you want
Ok, thanks I figured it out...used the KISS principle...
string redirectstr = String.Format(
"http://localhost/Gradebook/AcademicHonestyGrid.aspx?StudentID={0}&ClassSectionId={1}&uid={2}",
studid,
intSectionID,
HttpUtility.UrlEncode(encrypter.Encrypt(uinfo.ToXml())));
Response.Redirect(redirectstr );
works fine for what I am doing which is a test harness