locked
what is the best way to parse out some values from this query string? RRS feed

  • Question

  • string curUrl = HttpContext.Current.Request.RawUrl;

    >>

    "/umexpress/Contracts/ContractsReportPDF.aspx?rID=1&tID=5&psid=-1&ocol=10&otype=2&wiz=1&modal=dd5bf5ac-0c77-46ff-b5d5-9a5c866f3db8&guid=37874ad9-3e77-40c1-919c-d356477895ff&unqiue=414"

    <<

    I need to parse out -- &ocol=10&otype=2

    specifically I need the values 10 and 2, but I would like to get the var names also.  I was thinking about using curUrl.Split("&").  If that is a way to go -- having a problem with .Split("&").  How to implement that?  If there is a better way -- what would that be?


    Rich P

    Thursday, June 11, 2015 6:16 PM

Answers

  • I think that the expression HttpContext.Current.Request["ocol"] or HttpContext.Current.Request.QueryString["ocol"] will give you the first value.

    In order to enumerate the names, see HttpContext.Current.Request.QueryString.AllKeys.

    • Proposed as answer by Christopher84 Thursday, June 11, 2015 9:13 PM
    • Marked as answer by Just Karl Monday, June 22, 2015 9:51 PM
    Thursday, June 11, 2015 6:55 PM

All replies

  • I think that the expression HttpContext.Current.Request["ocol"] or HttpContext.Current.Request.QueryString["ocol"] will give you the first value.

    In order to enumerate the names, see HttpContext.Current.Request.QueryString.AllKeys.

    • Proposed as answer by Christopher84 Thursday, June 11, 2015 9:13 PM
    • Marked as answer by Just Karl Monday, June 22, 2015 9:51 PM
    Thursday, June 11, 2015 6:55 PM
  • Thursday, June 11, 2015 6:56 PM
  • I want to acknowledge Viorel_ as having the correct answer. However, I would like to add that you should check to see if it is null first.

    Either:

    if (Request.QueryString["ocol"] != null)
    {
    // do something with value
    value = Convert.ToInt32(Request.QueryString["ocol"]);
    }

    Or:

    value = Request.QueryString["ocol"] ?? "-1");

    Thursday, June 11, 2015 8:16 PM
  • Thank you all for the replies.  Turns out the app has its own homegrown parser (same as HttpContext.Current.Request["ocol"]  but also gets the int's from the parameters in the same step).   They asked me to use the homegrown one :).  But hey,  I also understand the homegrown version even better now.

    Rich P

    Thursday, June 11, 2015 9:34 PM
  • Hi Rich,

    Based on your code, HttpContext is under System.Web.dll. Your issue is related to asp.net/web technology. If you still not resolve this issue, for a better support on this please post your question to the asp.net forum below, http://forums.asp.net/<o:p></o:p>

    Best regards,

    Kristin


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    Friday, June 12, 2015 7:19 AM