I am learning basics of ASP.Net MVC 5. And I want to make a POST request to one of my controller action present in SecurityController
using Advanced rest Client
. But I am not able to do so. Please guide me.
Below is my attempt:
[HttpPost]
public ActionResult Hello(SampleMV viewModel)
{
return Content("Hello");
}
public class SampleMV
{
public int one { get; set; }
public int two { get; set; }
public int? three { get; set; }
}
Now what are the changes I need to make in my Advanced Rest Client?
Step 1. set request dropdown to POST
Step 2. in the raw payload section I added below explicitly:
one = 2, two = 3, three = 4 seperated by comma.
I am not sure if this is the right way.
Step 3. Do I need to put some content type or any other configuration. Currently, I am getting error as Resource not found
.
Here is the screenshot:
EDIT: Another Attempt
Your payload is malformed, the object definition in JSON should be { "one":2, "two":3, "three":4 }
404 means you have a routing problem. It's not related to the payload as that doesn't contribute to the route in any way. Assuming this method is SecurityController.Hello
, and you're utilizing the default route, a request to /security/hello
should go to the right place. As a result, it may be helpful to include your RouteConfig.cs in your question, as well.
Also, if you're utilizing an ApiController
, I think action names must follow the convention of starting with the request method. In other words, your Hello
action needs to be named PostHello
.
Once you get the routing sorted, the way you have the request body as JSON, now, should work fine. For future reference, "raw" would need to be application/x-www-form-urlencoded, i.e. one=1&two=2&three=3
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With