When passing form values as url encoding, only the names and values are encoded. The = and & are not.
Testing our asp.net mvc-5 Post action method using RESTClient firefox plugin, am i doing thing correcty?
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:
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
2 answers
Sort by: Most helpful
-
-
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
, orform-data
orraw
? 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:And this can get correct parameter like this:
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.