I’m trying to get a value from the URL and save it to a value but any attempt I’ve made has failed.
Here is an example of the URL:
https://localhost:44325/deals/PostDeal?id=101
I want to save the value of id
to DealsMasters.DealItemID
.
Here is what I have been working with
string baseUrl = string.Format("{0}://{1}",
HttpContext.Request.Scheme, HttpContext.Request.Host);
var query = HttpUtility.ParseQueryString(baseUrl.Query);
var var2 = query.Get("var2");
DealsMasters.DealItemID = var2;
2
Answers
I think you’re complicating it more than necessary.
HttpContext.Request
already has aQueryString
property you can get the individual parameters from:To address the errors you see:
The
baseUrl
you have defined is astring
and strings do not have aQuery
property so you cannot dobaseUrl.Query
.query.Get("id")
returns a string, since all parts of a URL are handled as strings. If you know that part of the URL is an integer, you need to convert/parse it, such as usingint.Parse(string)
like I did above.You only need to add a parameter which name is id to OnPostAsync: