Testing our asp.net mvc-5 Post action method using RESTClient firefox plugin, am i doing thing correcty?

john john 971 Reputation points
2022-10-17T08:58:04.587+00:00

I have the following Post action method inside my asp.net mvc-5 which accept username & password, and it acts as an api end point for our web application:

[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 follows:

251006-image.png

now i have noted the following 2 issues/observations, that i need to understand:-

1) if i remove the Header "application/x-www-form-urlencoded" >> the Post action method will be called but the parameters will be null..

2) since the password can contain characters such as "&" so i url encoded the parameters values before submitting them and it worked. so i can pass & as part of a parameter value.. but if i url encode the whole query string including the parameter names + the "&" for applying multiple parameters + the parameters values, then the action method will be called but the parameters values will be null..

any advice on the above please? thanks

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,536 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Bruce (SqlWork.com) 68,001 Reputation points
    2022-10-17T15:14:17.37+00:00

    When passing form values as url encoding, only the names and values are encoded. The = and & are not.

    0 comments No comments

  2. XuDong Peng-MSFT 10,836 Reputation points Microsoft Vendor
    2022-10-18T09:37:05.657+00:00

    Hi @john john ,

    if i remove the Header "application/x-www-form-urlencoded" >> the Post action method will be called but the parameters will be null.

    Did you choose another option when you removed this header? For example: none, or form-data or raw? Because at this point, I don't quite understand why it works correctly in your case. Under my test, it cannot call the post method unless I add parameters. like this:

    251552-image.png

    And this can get correct parameter like this:251523-image.png

    On this point, can you clarify it so that we can better understand your current question?

    In addition, if you need to pass multiple parameters, you can encapsulate it in a DTO (Data Transfer Object), which already contains all the parameters that need to be passed. Like this:

       public class User  
           {  
               public string username { get; set; }  
               public string password { get; set; }  
           }  
    

    And in controller, it should be :

       [HttpPost]  
       public ActionResult UserInfo(User userInfo)  
       {  
    

    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

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.