Skip to main content

ASP.NET QueryString Structure, Example and Limitations


Several time in ASP.NET applications we need to transfer data or information provided by user from one aspx page to another. To transfer data we can use several method, QueryString is one of them.
Query strings are data that is appended to the end of a page URL. They are commonly used to hold data like page numbers or search terms or other data that isn't confidential. Unlike ViewState and hidden fields, the user can see the values which the query string holds without using special operations like View Source.
QueryString is an example of GET method. QueryString is very handy when we need to pass low secure small content from one page to another page. But if we need to pass secure information like credit card number etc or large amount of data then we can't use QueryString.


Structure of Query String
Query strings are appended to the end of a URL. First a question mark is appended to the URL's end and then every parameter that we want to hold in the query string. The parameters declare the parameter name followed by = symbol which followed by the data to hold. Every parameter is separated with the ampersand symbol.
You should always use the HttpUtility.UrlEncode method on the data itself before appending it.



Here are example of how we can use QueryString across pages.

QueryString example [C#]

Pass the QueryString to another page as:
http://www.abc.com?Default.aspx?fname=Sandeep

Here we have passed fname as QueryString to Default.aspx page. Now we'll access QueryString's value on page's code behind(In Default.aspx.cs) as:

    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["fname"] != null)
            {
                String firstName = Request.QueryString["fname"];
                Response.Write("First Name is " + firstName);
            }
        }
    }
Result:
    First Name is Sandeep


Two query string parameters

Now we have familiar with QueryString, Here we see how you can test two query string URL parameters. Another common requirement is to test two different query strings. You may have to use either one or both at once. 


Pass the two QueryString as:
http://www.abc.com?Default.aspx?fname=Sandeep&lname=Tada

In above line we pass two querystring with &, we can pass more querystring like this if we required.
?fname=Sandeep&lname=Tada


Now we'll access QueryString's value on page's code behind(In Default.aspx.cs) as:


  public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (Request.QueryString["fname"] != null &&           Request.QueryString["lname"] != null)
            {
                String firstName = Request.QueryString["fname"];
                String lastName = Request.QueryString["lname"];
                Response.Write("Name is " + firstName + " " + lastName);
            }
        }
    }

Result:
    Name is Sandeep Tada
Query String Limitations
You can use query string technique when passing from one page to another but that is all. If the first page need to pass non secure data(as mentioned above) to the other page it can build a URL with a query string and then redirect. You should always keep in mind that a query string isn't secure and therefore always validate the data you received. There are a few browser limitation when using query strings. For example, there are browsers that impose a length limitation on the query string. 
Another limitation is that query strings are passed only in HTTP GET command. 
Maximum length of Querystring in ASP.NET
Maximum length of Querystring is based on the browser not depend upon the asp.net. here is some information

Maximum length of a querystring in IE 4.0 and above is ~2048 characters  

IE. 4,5,6,7, - 2,048 characters. 

Opera supports - 4050 characters. 

Netscape 6 supports - 2000 characters. 

Firefox Supports - 6000 characters  

Cheers!!!! हैप्पी प्रोग्रम्मिंग :)

Comments

Popular posts from this blog

Error 405 : ASP.NET Core Web API PUT and DELETE Methods not allowed

Recently, while working with .Net core API I came across the issue of “Error 405 — Methods not Allowed” After some research, I found out that both GET and POST requests working fine but neither PUT nor DELETE requests working. Another thing is that the PUT and DELETE request was also working fine on my local machine but failed when we host on our Azure server. When I explored the issue on the web it led me to the conclusion that WebDAVModule seems to set PUT and DELETE request methods disabled by default and due to that PUT and DELETE throw 405 errors. To make the PUT and DELETE requests work, we need to override the WebDAVModule setting in web.config file by adding the below settings under “ system.webServer ”. < system.webServer >   < modules runAllManagedModulesForAllRequests = " false " >     < remove name = " WebDAVModule " />   </ modules > </ system.webServer > There may be 2 web.config files in y...

C#: Merging Excel cells with NPOI HSSFWorkbook

In this post we’ll see how to merge the two or more cell with each other while creating the excel sheet using NPOI . Mentioned below is code to merge multiple cells, in this example we are merging first cell to fifth cell of first row (you can adjust row or cell range by passing particular parameters in CellRangeAddress). //Created new Workbook var hwb = new NPOI.HSSF.UserModel. HSSFWorkbook (); //Create worksheet with name. var sheet = hwb.CreateSheet( "new sheet" ); //Create row and cell. var row = sheet.CreateRow(0); var cell = row.CreateCell(0); ; //Set text inside cell cell.SetCellValue( "This is Merged cell" ); cell.CellStyle.WrapText = true ; //define cell range address // parameters: -> first row to last and first cell to last cell var cra = new NPOI.SS.Util. CellRangeAddress (0, 0, 0, 4); //Add merged region to sheet. sheet.AddMergedRegion(cra); Hope this solution helps you J

How to set Swagger as the default start page for API hosted on the Azure web app?

I created an Asp.Net Core 2.x Web API and configured Swagger on it, below is the code added in Configure method under Startup.cs file, for full swagger configuration, check here //Add swagger configuration app.UseSwagger(); app.UseSwaggerUI(c => {     c.SwaggerEndpoint( "../swagger/v1/swagger.json" , "Test API V1" ); }); On my local machine when I run the API it is automatically redirected to the Swagger page. However, when I hosted this API as an Azure web app it is not redirecting directly to the Swagger and to access the swagger, I had to append /swagger in the URL, for example, https://testapi.azurewebsites.net/swagger/ Solution: Set RoutePrefix to string.Empty under app.UseSwaggerUI like below: app.UseSwaggerUI(c => {     c.SwaggerEndpoint( "../swagger/v1/swagger.json" , "Test API V1" );      c.RoutePrefix = string .Empty; // Set Swagger UI at apps root }); And that’s it, now when you b...