Jinto John Jinto John - 6 months ago 125
Ajax Question

Pass special character through query string in ASP.NET MVC 4

When I trying to pass some special characters though query string in URL property of ajax post method, it seems to be some error happened. That means I won't get correct special characters when hitting controller action in ASP.NET MVC 4.

My code is

var temp = temp@#$#%#%#979

url = (Controller/Action/id/name?departname=+temp);

Answer

use HttpUtility.UrlEncode for encoding the parameter

var temp = temp@#$#%#%#979
HttpUtility.UrlEncode(temp)
url = (Controller/Action/id/name?departname=+temp);

and use Server.UrlDecode for decoding the parameter in controller

var Depart = Server.UrlDecode(Request.QueryString["DepartmentName"]);