开发者

Why in URL <character before> #<charecter after> reads only <character before > only i Query String?

If I pass

const string strEditParams = "TaskID={0}&TaskName={1}";
                string strEditValues = string.Format(strEditParams,
                                                    lbl.Text
                                                    , "$#hello"                                                    
                                                 开发者_如何学Python   );
                lnkBtn.Attributes.Add("onClick", "return OpenEditTask('test.aspx?" + strEditValues + "')");

from page1.aspx to test.aspx, and in the page load of test.aspx if I do

if(!IsPostBack)
        {
            var pp = Request["TaskName"].ToString();

        }

I get only $.

Why in query string i am getting only $ in this case and not the full values of "TaskName" (characters before #) # and how to overcome this?

Thanks


In URLs, the # character introduces a fragment identifier. It has to be encoded to be used in the query string itself.

You can either encode it yourself:

string strEditValues = string.Format(strEditParams, lbl.Text, "$%23hello");

Or use HttpServerUtility.UrlEncode():

string strEditValues = string.Format(strEditParams, lbl.Text,
    Server.UrlEncode("$#hello"));


That is because the # is a special character in URLs, it marks the fragment ID.

You have to encode it as %23 to pass it as a parameter or use UrlEncode instead.

Your code could look like this:

lnkBtn.Attributes.Add("onClick", "return OpenEditTask('test.aspx?" 
    + Server.UrlEncode(strEditValues) + "')");

That would correctly encode any special characters from strEditValues.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜