3

When I try to pass some special characters, though query string in the URL property of the 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);
4
  • are you making a ajax post from javascript? or a form? Commented May 21, 2016 at 20:33
  • Unsafe Characters in URLs Commented May 22, 2016 at 6:46
  • @StephenMuecke..i had to pass special charoctor as query string in a real case scenario. Commented Jun 6, 2017 at 9:18
  • @BenG.. yes from a ajax post method Commented Jun 6, 2017 at 9:29

3 Answers 3

2

request from a script function from a cshtml formajax

If you are making your ajax request from javascript, then use encodeURIComponent:-

var temp = encodeURIComponent('temp@#$#%#%#979'); //temp%40%23%24%23%25%23%25%23979
Sign up to request clarification or add additional context in comments.

Comments

2

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"]);

Comments

0

I'm not familiar with ASP.net, however most languages require you to URL encode strings that are meant to be used as part of a URL.

For example: https://msdn.microsoft.com/en-us/library/4fkewx0t%28v=vs.110%29.aspx

I hope this helps get you moving in the right direction.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.