pass a parameters to a Post action method which contain "&" inside our aps.net mvc-5

john john 1,021 Reputation points
2022-10-17T07:59:19.35+00:00

I have the following Post action method which accept username & password:-

 [HttpPost]  
        public ActionResult UserInfo(string username,string password)  
        {  

and i am testing it using FireFox RESTCleint add-in. where i am passing the URL + the Header as "application/x-www-form-urlencoded" + the username/password, as follow:-

251031-image.png

now things are working well, but in case the username or the password contain this character "&" then the logic will break, as seems the asp.net will assume that what comes after "&" is a new parameter and not part of the parameter value, so how i can fix this issue? and is it an issue with my code or with the FireFox plugin or with encoding the parameters?

Second question, if i want to specify the technical specification for calling this Acton method from external applications, then what is the data encoding needed for the parameters? Thanks

Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Anonymous
    2022-10-18T03:32:42.007+00:00

    Hi @john john ,

    Based on your description, I think this problem is mainly related to url encoding, not the plugin you mentioned. You need to know that parameter key-value pairs in the QueryString are separated by ** & **, so you need to take this into account when simulating requests in postman.

    And in this part, the following characters are reserved characters (separated by space), you need to use the corresponding encoding in the HTTP Request body to display them.

       ! * ' ( ) ; : @ & = + $ , / ? % # [ ]  
    

    In your case, the encoding for ** & ** is ** %26 **. In your case, the url should be something like this: https://localhost:44320/api/userinfo?username=test%26test&password=$$%26$$.

    And for more details on this, I suggest you refer to the discussion in this thread, I think it will help you: application/x-www-form-urlencoded or multipart/form-data?.

    Regarding the second question you mentioned, I suggest you use json format to pass parameters if you want to set parameters in request body. And in this case, you also need to encapsulate these parameters into DTO objects.

    Best regards,
    Xudong Peng


    If the answer is the right solution, please click "Accept Answer" and kindly upvote. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

  2. Bruce (SqlWork.com) 78,086 Reputation points Volunteer Moderator
    2022-10-21T16:17:09.703+00:00

    the url encoding rules:

    https://www.w3schools.com/tags/ref_urlencode.ASP

    trival js implementation:

    var data = {   
      userName: "john",   
      password: "hello&there"   
    }   
       
    function formEncode(data)   
    {   
      var body = "";   
      var sep = "";   
      for (var n in data) {   
        body += sep + encodeURIComponent(n) + "=" + encodeURIComponent(data[n]);   
        sep = "&";   
      }   
      return body;   
    }   
       
    alert(formEncode(data));   
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.