How to parse URL with multiple values into multiple int

Viewed 300

I have a query string that I need to split into 2 separate int and I'm having a hard time getting it to work and a hard time understanding other examples. Basically I want to split the date part of a url for example - www.website.com?2019&9. Here is what I have been trying to get to work. I know there is definitely a more elegant way to do this but I'm new to using query strings.

        string QueryString = Request.QueryString.ToString();
        int [] QueryInt = new int[2];
        int k = 0;
        string[] QuerySplit = QueryString.Split('&');
        foreach (var item in QuerySplit)
        {
            int.TryParse(QueryString, out QueryInt[k]);
            k++;
        }
1 Answers

I ended up using Request.QueryString["Param"]; as suggested by itsme86. This isn't the prettiest solution and I'm sure there is a better one but it works and thats the most important thing.

Parameters in URL should look like

exampleurl.com/?year=2019&month=9.

    string monthQuery = Request.QueryString["month"];
    int monthVal = 0;
    int.TryParse(monthQuery, out monthVal);
    string yearQuery = Request.QueryString["year"];
    int yearVal = 0;
    int.TryParse(yearQuery, out yearVal);
Related