Get Url Parameters from a String in .Net

Get URL parameters from a string in .NET

Use static ParseQueryString method of System.Web.HttpUtility class that returns NameValueCollection.

Uri myUri = new Uri("http://www.example.com?param1=good¶m2=bad");
string param1 = HttpUtility.ParseQueryString(myUri.Query).Get("param1");

Check documentation at http://msdn.microsoft.com/en-us/library/ms150046.aspx

How to get the parameter from a relative URL string in C#?


 int idx = url.IndexOf('?');
string query = idx >= 0 ? url.Substring(idx) : "";
HttpUtility.ParseQueryString(query).Get("ACTION");

extract query string from a URL string

It's not clear why you don't want to use HttpUtility. You could always add a reference to System.Web and use it:

var parsedQuery = HttpUtility.ParseQueryString(input);
Console.WriteLine(parsedQuery["q"]);

If that's not an option then perhaps this approach will help:

var query = input.Split('&')
.Single(s => s.StartsWith("q="))
.Substring(2);
Console.WriteLine(query);

It splits on & and looks for the single split result that begins with "q=" and takes the substring at position 2 to return everything after the = sign. The assumption is that there will be a single match, which seems reasonable for this case, otherwise an exception will be thrown. If that's not the case then replace Single with Where, loop over the results and perform the same substring operation in the loop.

EDIT: to cover the scenario mentioned in the comments this updated version can be used:

int index = input.IndexOf('?');
var query = input.Substring(index + 1)
.Split('&')
.SingleOrDefault(s => s.StartsWith("q="));

if (query != null)
Console.WriteLine(query.Substring(2));

How to read the query string params of a ASP.NET raw URL?

This is probably what you're after

  Uri theRealURL = new Uri(HttpContext.Current.Request.Url.Scheme + "://" +   HttpContext.Current.Request.Url.Authority + HttpContext.Current.Request.RawUrl);

string yourValue= HttpUtility.ParseQueryString(theRealURL.Query).Get("yourParm");

Get individual query parameters from Uri

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));

How to get all url parameters in asp.net mvc?

You can use the HttpContext Type to grab the query string

var context = HttpContext.Current;

then, you can grab the entire query string:

var queryString = context.Request.QueryString

// "bar=baz&type=promotion"

or, you can get a list of objects:

var query = context.Request.Query.Select(_ => new
{
Key = _.Key.ToString(),
Value = _.Value.ToString()
});

// { Key = "bar", Value = "baz" }
// { Key = "type", Value = "promotion" }

or, you could make a dictionary:

Dictionary<string, string>queryKvp = context.Request.GetQueryNameValuePairs()
.ToDictionary(_=> _.Key, _=> _.Value, StringComparer.OrdinalIgnoreCase);

// queryKvp["bar"] = "baz"
// queryKvp["type"] = "promotion"

How to extract a value from a URL query string in C#?

token=(\d+) should do the trick

To cater for aplhas as well you can do either:

token=([a-zA-Z0-9+/=]+) to explicitly match on the characters you expect. This matches "token=" and then captures all following characters that match the character class, that is, a-z, A-Z, 0-9, +, / and =.

or

token=([^&#]+) to match any character except for the ones you know can finish the token. This matches "token=" and then captures all characters until the first &, # or the end of the string.

How to get a parameter from a URL encoded?

The encoding is happening in your first line already:

string queryString = new Uri(URL).Query;

Presumably you want to avoid writing your own code to extract the Query part from a URL. Which is sensible. You can still rely on the Uri class to do the parsing for you:

var uri=new Uri(URL);
var queryString= URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');

———————————————————————————————————

(

var URL="http://a/b/?d= @£$%% sdf we  456 7 5?367";
var uri=new Uri(URL);
Console.WriteLine(uri.Query); // Already url-encoded by the Uri constructor
var queryString = URL.Replace(uri.GetLeftPart(UriPartial.Path), "").TrimStart('?');
Console.WriteLine(System.Web.HttpUtility.ParseQueryString(queryString)); //Not encoded!

)

(As an aside, LinqPad helps)



Related Topics



Leave a reply



Submit