Jinto John Jinto John - 1 year ago 242
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 Source

use HttpUtility.UrlEncode for encoding the parameter

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

and use Server.UrlDecode for decoding the parameter in controller

var Depart = Server.UrlDecode(Request.QueryString["DepartmentName"]);
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download