How to get data from url

I have a URL http: //localhost/TradeCredits/UnderWriter/EditQuote.aspx? QOT_ID = 1 I want to get the QOT_ID from the URL. Please suggest how to do this.

+2


a source to share


5 answers


You can use this line of code:

int id = Convert.ToInt32(Request.QueryString["QOT_ID"]);

      



Or this if you want to do the correct check:

int id;
if (int.TryParse(Request.QueryString["QOT_ID"], out id)) {
    // Do something with the id variable
} else {
    // Do something when QOT_ID cannot be parsed into an int
}

      

+5


a source


If you have URL

, as you mentioned in your question, that cannot be connected to the current request, you can do it like this:

string url = "http://localhost/TradeCredits/UnderWriter/EditQuote.aspx?QOT_ID=1";
Uri uri = new Uri(url);
var parameters = HttpUtility.ParseQueryString(uri.Query);
var id = parameters["QOT_ID"];

      



and the id

variable contains your parameter value.

+3


a source


The collection Request.QueryString

contains all the URL parameters - each of them can be accessed by name:

var val = Request.QueryString["QOT_ID"];

      

All return values ​​are variables string

, so you may need to cast them to the appropriate type.

0


a source


Request.QueryString provides an array of string variables sent to the client

Request.QueryString["QOT_ID"] 

      

See documentation

0


a source


QOT_ID can be zero or a combination of letters

int id = Request.QueryString["QOT_ID"] != null && Int32.TryParse(Request.QueryString["QOT_ID"]) ? int.Parse(Request.QueryString["QOT_ID"]) : -1;

      

Not 100% sure if you can safely pass null to Int32.TryParse.

0


a source







All Articles