How to Pass To Query String In Asp.net

How to Pass To Query String In Asp.net

Asp.net Query String parameters. How to pass QueryString parameter in Asp.net is explained here by Satalaj. In this article we will pass multiple query string parameters to URL and retrieve their values. As a website developer you may have come across situation to pass the values via query string to API server URL or your own web URL. Let's see how to encode the Query String values and pass it to URL.

Asp.net QueryString parameter

In Asp.net query string parameters are separated by & and you can pass as many as parameters you want. Yes, there is limit at browser side. If you are accepting unlimited values via query string, you need to configuring HTTP Runtime settings of machine config or web.config.

using System;
using System.Web;


public partial class QueryString_Example : System.Web.UI.Page
{
  protected void Page_Load(object sender, EventArgs e)
  {
    string userName = Request.QueryString["User"];
    string PhoneNumber = Request.QueryString["Phone"];
    Label1.Text = "<b>Query string Values are:</b></br> User Name " + userName + "</br>Phone Number " + PhoneNumber;


  }
  protected void Button1_Click(object sender, EventArgs e)
  {

    string phone = HttpUtility.UrlEncode("(+091)-2568-7891.");
    string user = HttpUtility.UrlEncode("Satalaj More.");

    string absoluteurl = Request.Url.AbsolutePath;

    string url = absoluteurl + "?" + "Phone=" + phone + "&User=" + user;

    Response.Redirect(url);


  }
}

? is used to separate absolute URL from query string parameters.

& is used to separate query string parameters.

Output of the URL is: http://localhost:3716/web.Demo/HtmlTextWriter_Example.aspx?Phone=(%2b091)-2568-7891.&User=Satalaj+More.

You must encode the parameter before passing it to query string. Otherwise, your request will get terminated. The encoded output of phone number (+091)-2568-7891. is (%2b091)-2568-7891.

Important Note: Don’t use UrlDecode method after receiving the values from query string. Many developers do such mistake. Query String method it self's gives you decoded content.

VB.net QueryString parameter.
Imports System
Imports System.Web


Public Partial Class QueryString_Example
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)
		Dim userName As String = Request.QueryString("User")
		Dim PhoneNumber As String = Request.QueryString("Phone")
		Label1.Text = "Query string Values are:
User Name " + userName + "
Phone Number " + PhoneNumber End Sub Protected Sub Button1_Click(sender As Object, e As EventArgs) Dim phone As String = HttpUtility.UrlEncode("(+091)-2568-7891.") Dim user As String = HttpUtility.UrlEncode("Satalaj More.") Dim absoluteurl As String = Request.Url.AbsolutePath Dim url As String = absoluteurl + "?" + "Phone=" + phone + "&User=" + user Response.Redirect(url) End Sub End Class

Tags:

Asp.net Convert String To ImageC# DateTime ToString()Jquery Trim String[Asp.net] Get current Page URL absolute URI and query string parameters.Asp.net Urldecode QueryString.net String vs stringasp net image uploadC# foreach loopReplace string in C#ASp.net 2.0 step by step Membership Provider ConnectionStrings or appSettings in Asp.net

Author

My name is Satalaj, but people call me Sat. Here is my homepage: . I live in Pune, PN and work as a Software Engineer. I'm former MVP in ASP.net year 2010.
Disclaimer: Views or opinion expressed here are my personal research and it has nothing to do with my employer. You are free to use the code, ideas/hints in your projects. However, you should not copy and paste my original content to other web sites. Feel free to copy or extend the code.
If you want to fight with me, this website is not for you.
 

I'm Satalaj.