Search code examples
asp.netquery-stringimageurl

Read query string paramter from Image Url [asp.net]


I have created a dynamic image using Generic Handler[.ashx] and then assigning the same to the Image on my user control from code behind.

string capt = //some random logic value.
imgCaptcha.ImageUrl = "~/BO/term.ashx?param=" + capt;

Now on some button event on same usercontrol, I want to read the query parameter of the ImageUrl

 protected void btnVerify_Click(object sender, EventArgs e)
 {
     //something like this
     string param = Request.QueryString["param"]

 }

But Request.QueryString wont give me anything as the usercontrol is added on some .aspx page so Request path will be the same .aspx page which do not have the query parameters.

But imgCaptcha.ImageUrl give me some path which is ~/BO/term.ashx?param=123456.

Now I want to read this information using some .Net standard class. Is there anything which will solve my query?

Note : I am not much interested in using string methods.


Solution

  • you can generate uri and retrieve query string parameters however string split is better:

     Uri uri = new System.Uri(Page.Request.Url, VirtualPathUtility.ToAbsolute(Image1.ImageUrl));
     var parameters = System.Web.HttpUtility.ParseQueryString(uri.Query);
     string value = parameters["param"];