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;
CodePudding user response:
I think you're complicating it more than necessary. HttpContext.Request
already has a QueryString
property you can get the individual parameters from:
var idParam = HttpContext.Request.QueryString["id"];
DealsMasters.DealItemID = int.Parse(idParam); // Note: this will fail if 'id' is not an integer
To address the errors you see:
'string' does not contain a definition for 'Query' [...]
The baseUrl
you have defined is a string
and strings do not have a Query
property so you cannot do baseUrl.Query
.
Cannot implicitly convert type 'string' to 'int'
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 using int.Parse(string)
like I did above.
CodePudding user response:
You only need to add a parameter which name is id to OnPostAsync:
public async Task<IActionResult> OnPostAsync(int id)
{
DealsMasters.DealItemID = id;
...
}