Anura Adhikari
Anura Adhikari

Reputation: 315

How to retrieve query string value which contains Hash character

In my Asp.net (C#) application, I need to read three parameters from the query string (Item, Code and Location). Url is given below.

http://localhost/Reports?ITEM=A#1234&CODE=0013&LOCATION=LOCA#001

I am reading it like this.

            _code = Request["CODE"]; //value should be 0013
            _item = Request["ITEM"]; //value should be A#1234
            _location = Request["LOCATION"]; //value should be LOCA#001

But after # character, nothing retrievable to the variables. Our database contains lots of data items with hash(#) character. Any idea how to read with # ?

Upvotes: 0

Views: 305

Answers (1)

NMK
NMK

Reputation: 1020

Use URL encoding for the param values urlencode

To encode or decode values outside of a web application, use the WebUtility class.

using System;
using System.Net;

    public class Program
    {
        public static void Main()
        {

            string urlValue = "ITEM="+WebUtility.UrlEncode("A#1234") + "&CODE=0013&LOCATION=" + WebUtility.UrlEncode("LOCA#001");
            Console.WriteLine(urlValue);
            Console.WriteLine(WebUtility.UrlDecode(urlValue));

        }
    }

Upvotes: 1

Related Questions