How to split a url c# [duplicate] - c#

This question already has answers here:
Get URL parameters from a string in .NET
(17 answers)
Closed 4 years ago.
I have a uri string like: http://example.com/file?a=1&b=2&c=string%20param
Is there an existing function that would convert query parameter string into a dictionary same way as ASP.NET Context.Request does it.
I'm writing a console app and not a web-service so there is no Context.Request to parse the URL for me.
I know that it's pretty easy to crack the query string myself but I'd rather use a FCL function is if exists.

Use this:
string uri = ...;
string queryString = new System.Uri(uri).Query;
var queryDictionary = System.Web.HttpUtility.ParseQueryString(queryString);
This code by Tejs isn't the 'proper' way to get the query string from the URI:
string.Join(string.Empty, uri.Split('?').Skip(1));

You can use:
var queryString = url.Substring(url.IndexOf('?')).Split('#')[0]
System.Web.HttpUtility.ParseQueryString(queryString)
MSDN

This should work:
string url = "http://example.com/file?a=1&b=2&c=string%20param";
string querystring = url.Substring(url.IndexOf('?'));
System.Collections.Specialized.NameValueCollection parameters =
System.Web.HttpUtility.ParseQueryString(querystring);
According to MSDN. Not the exact collectiontype you are looking for, but nevertheless useful.
Edit: Apparently, if you supply the complete url to ParseQueryString it will add 'http://example.com/file?a' as the first key of the collection. Since that is probably not what you want, I added the substring to get only the relevant part of the url.

I had to do this for a modern windows app. I used the following:
public static class UriExtensions
{
private static readonly Regex _regex = new Regex(#"[?&](\w[\w.]*)=([^?&]+)");
public static IReadOnlyDictionary<string, string> ParseQueryString(this Uri uri)
{
var match = _regex.Match(uri.PathAndQuery);
var paramaters = new Dictionary<string, string>();
while (match.Success)
{
paramaters.Add(match.Groups[1].Value, match.Groups[2].Value);
match = match.NextMatch();
}
return paramaters;
}
}

Have a look at HttpUtility.ParseQueryString() It'll give you a NameValueCollection instead of a dictionary, but should still do what you need.
The other option is to use string.Split().
string url = #"http://example.com/file?a=1&b=2&c=string%20param";
string[] parts = url.Split(new char[] {'?','&'});
///parts[0] now contains http://example.com/file
///parts[1] = "a=1"
///parts[2] = "b=2"
///parts[3] = "c=string%20param"

For isolated projects, where dependencies must be kept to a minimum, I found myself using this implementation:
var arguments = uri.Query
.Substring(1) // Remove '?'
.Split('&')
.Select(q => q.Split('='))
.ToDictionary(q => q.FirstOrDefault(), q => q.Skip(1).FirstOrDefault());
Do note, however, that I do not handle encoded strings of any kind, as I was using this in a controlled setting, where encoding issues would be a coding error on the server side that should be fixed.

In a single line of code:
string xyz = Uri.UnescapeDataString(HttpUtility.ParseQueryString(Request.QueryString.ToString()).Get("XYZ"));

Microsoft Azure offers a framework that makes it easy to perform this.
http://azure.github.io/azure-mobile-services/iOS/v2/Classes/MSTable.html#//api/name/readWithQueryString:completion:

You could reference System.Web in your console application and then look for the Utility functions that split the URL parameters.

Related

How to pass one local string into the middle of another string as a variable? .NET C#

In the below example, how to change the local QNARESULTHERE.json file with the name of the qnaResult?
public async Task ITSupportIntent(IDialogContext context, LuisResult result)
{
var qnaResult = itKB.GetAnswer(result.Query);
if (qnaResult.StartsWith("CARD"))
{
var reply = context.MakeMessage();
try
{
string json = File.ReadAllText(HttpContext.Current.Request.MapPath("~\\AdaptiveCards\\QNARESULTHERE.json"));
Sorry this question is all over the place.
Context:
The actual variable i needed was the QnAAnswer i believe as referenced here: Integrate QnA Maker and LUIS to distribute your knowledge base
It's a little unclear exactly what value you are trying to get from the result, but assuming LuisResult has a FileName property on it, as an example, you can use string interpolation (available since C# 7), like this:
string relativePath = $"~\\AdaptiveCards\\{result.FileName}.json";
You may also find that the #-style verbatim string syntax works better here, since it means you don't have to escape backslashes:
string relativePath = $#"~\AdaptiveCards\{result.FileName}.json";
If you're using an older version of C#, you can also use string.Format or just plain old string concatenation:
string relativePath = string.Format(#"~\AdaptiveCards\{0}.json", result.FileName);
string relativePath = #"~\AdaptiveCards\" + result.FileName + ".json";
Whichever you choose, you will of course want to pass the resulting value along like you were doing.
string json = File.ReadAllText(HttpContext.Current.Request.MapPath(relativePath));
You can use string.Format() like so:
string json = File.ReadAllText(HttpContext.Current.Request.MapPath(string.Format(#"~\AdaptiveCards\{0}.json", qnaResult));
Above is assuming qnaResult is a string that contains the file name you want. If it's a class instance, then use the appropriate property of it that contains the file name.

Extract Parameter from Url

I have a winform application, and I would like to parse a string that represent an URL to extract some parameters.
a sample of the URL is this:
http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e
the parameter I would like to extract is 271443634510 (that is, the last part of the path before the query string).
Any idea ho how this can be done?
You can use Uri.Segments, which splits up the stuff after your domain into an array that includes, for your example:
/
itm/
Sector-Watch/
271443634510
So all you need to get is the item at index 3. Working example:
string url = "http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
Uri uri = new Uri(url);
var whatYouWant = uri.Segments[3];
You can do this:
string url = "http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
string parameter = Regex.Match(url,"\d+(?=\?)|(?!/)\d+$").Value;
You can simply use Split function (tested and verified):
string MyUrl="http://www.mysite.com/itm/Sector-Watch/271443634510?pt=Orologi_da_Polso&hash=item3f334d294e";
string str=MyUrl.Split('/').Last().Split('?').First();

Replace URL in a string

I'm a beginner in C# and I have the following string,
string url = "svn1/dev";
along with,
string urlMod = "ato-svn3-sslv3.of.lan/svn/dev"
I want to replace svn1 in url with "ato-svn3-sslv3.of.lan"
Although your question still has some inconsistent statements, I believe String.Replace is what you are looking for:
http://msdn.microsoft.com/en-us/library/fk49wtc1.aspx
url = url.Replace("svn1","ato-svn3-sslv3.of.lan");
Strings are immutable so you need to assign the return value to a variable:
string replacement = "ato-svn3-sslv3.of.lan";
url = url.Replace("svn1", replacement);
You can use the string method replace.
url = url.Replace("svn1", urlMod)
I think you need this:
string url = "svn1/dev";
string anotherUrl = "ato-svn3-sslv3.of.lan/svn/dev";
string toBeReplaced = anotherUrl.Split('/')[0];
url = url.Replace("svn1", toBeReplaced);
It uses split method and replace method.

How can I parse HTTP urls in C#?

My requirement is to parse Http Urls and call functions accordingly. In my current implementation, I am using nested if-else statement which i think is not an optimized way. Can you suggest some other efficient approch?
Urls are like these:
server/func1
server/func1/SubFunc1
server/func1/SubFunc2
server/func2/SubFunc1
server/func2/SubFunc2
I think you can get a lot of use out of the System.Uri class. Feed it a URI and you can pull out pieces in a number of arrangements.
Some examples:
Uri myUri = new Uri("http://server:8080/func2/SubFunc2?query=somevalue");
// Get host part (host name or address and port). Returns "server:8080".
string hostpart = myUri.Authority;
// Get path and query string parts. Returns "/func2/SubFunc2?query=somevalue".
string pathpart = myUri.PathAndQuery;
// Get path components. Trailing separators. Returns { "/", "func2/", "sunFunc2" }.
string[] pathsegments = myUri.Segments;
// Get query string. Returns "?query=somevalue".
string querystring = myUri.Query;
This might come as a bit of a late answer but I found myself recently trying to parse some URLs and I went along using a combination of Uri and System.Web.HttpUtility as seen here, my URLs were like http://one-domain.com/some/segments/{param1}?param2=x.... so this is what I did:
var uri = new Uri(myUrl);
string param1 = uri.Segments.Last();
var parameters = HttpUtility.ParseQueryString(uri.Query);
string param2 = parameters["param2"];
note that in both cases you'll be working with strings, and be specially weary when working with segments.
I combined the split in Suncat2000's answer with string splitting to get at interesting features of the URL. I am passing in a full Uri including https: etc. from another page as the navigation argument e.Parameter:
Uri playlistUri = (Uri)e.Parameter;
string youtubePlaylistUnParsed = playlistUri.Query;
char delimiterChar = '=';
string[] sections = youtubePlaylistUnParsed.Split(delimiterChar);
string YoutubePlaylist = sections[1];
This gets me the playlist in the PLs__ etc. form for use in the Google APIs.

.NET Regular expression for querystring value

I need to strip out any "&id=SomeValue" from a Url.PathAndQuery. Where SomeValue could be an int or a string. And it may or may not be followed by another ampersand.
So it could be
somepage.aspx?cat=22&id=SomeId&param2=4
or
somepage.aspx?cat=tect&id=450
I want to be left with
somepage.aspx?cat=22&param2=4
or
somepage.aspx?cat=tect
Just going off the top of my head...
string url = "somepage.aspx?cat=22&id=SomeId&param2=4";
Regex regex = new Regex("([\?\&])id=[^\?\&]+");
url = regex.replace(url, "\1");
System.Diagnostics.Debug.WriteLine("url = " + url);
Update 2010-03-05 11:12 PM PST
I've been shamed by a comment into actually testing my code. What are you, my QA department? Here's a working example using MSTest.
Regex regex = new Regex(#"([\?\&])id=[^\&]+[\&]?");
[TestMethod]
public void RegexReplacesParameterInMiddle()
{
string url = "somepage.aspx?cat=22&id=SomeId&param2=4";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?cat=22&param2=4",url);
}
[TestMethod]
public void RegexReplacesParameterInFront()
{
string url = "somepage.aspx?id=SomeId&cat=22&param2=4";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?cat=22&param2=4", url);
}
[TestMethod]
public void RegexReplacesParameterAtEnd()
{
string url = "somepage.aspx?cat=22&param2=4&id=SomeId";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?cat=22&param2=4&", url);
}
[TestMethod]
public void RegexReplacesSoleParameter()
{
string url = "somepage.aspx?id=SomeId";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?", url);
}
public void RegexIgnoresMissingParameter()
{
string url = "somepage.aspx?foo=bar&blet=monkey";
url = regex.Replace(url, "$1");
Assert.AreEqual("somepage.aspx?foo=bar&blet=monkey", url);
}
The regex, interpreted, says:
Look for a "?" or an "&" character (and store it as a backreference)
followed by "id="
followed by one or more non-"&" characters.
optionally followed by another "&"
Then replace that expression with the backreference, so you don't lose your initial ?/&.
note -- as you can see from the tests, this emits a trailing ? or & when the replaced parameter is the only one or the last one, respectively. You could use string methods to get rid of that, though if somebody knows how to keep them out of the result using only regular expressions it would be excellent to see.
If bad things could happen (e.g., security-wise) if an "id=" parameter were missed by the regular expression, then you also need to worry that the query string might contain a hexadecimal urlencoded equivalent, which the regular expression will not recognize. For example, "id" is "%69%64". Also consider the effects of different capitalizations of "id" on your program. My opinion in this situtation is that you read the RFCs and build a complete class that can do transformations in both directions from a set of name-value pairs to a query strings. System.Uri will not do this. if you are running inside an ASP.NET application, you might investigate if HttpUtility.ParseQueryString is sufficient.
I would first parse the Querystring to Strongly typed values, then I would check using Regex if I needed to.
C# ASP.NET QueryString parser

Categories