ASP.NET MVC JSON body with POST

I am new to ASP.NET MVC and studying. So far, I have figured out how to create a JSON object and return it in response to a request. However, I cannot pass the JSON body as part of the POST request, as I used to use Java .

Here is the code how I did it -

 @Path("/storeMovement") @POST @Consumes("application/json") @Produces("application/json") public String storeTrace(String json) { JSONObject response = new JSONObject(); JSONParser parser = new JSONParser(); String ret = ""; try { Object obj = parser.parse(json); JSONObject jsonObj = (JSONObject) obj; RecordMovement re = new RecordMovement((double) jsonObj.get("longitude"), (double) jsonObj.get("latitude"), (long) jsonObj.get("IMSI")); ret = re.Store(); // Clear object re = null; System.gc(); response.put("status", ret); } catch (Exception e) { response.put("status", "fail " + e.toString()); } return response.toJSONString(); } 

I tried the same in the ASP.NET action method, but the value of the string a parameter is null , as seen during debugging. Here the action method code is

 public string Search(string a) { JObject x = new JObject(); x.Add("Name", a); return x.ToString(); } 

It works fine when I use Object (like a book) in the same way -

 public string Search(Book a) { JObject x = new JObject(); x.Add("Name", a.Name); return x.ToString(); } 

In this case, the name of the book is de-serialized just fine, as I expected. The class definition for the Book class is

 public class Book { public int ID { get; set; } public string Name { get; set; } } 

Can someone please tell me what I'm doing wrong? Is there no way to take a string and then de-serialize? I would like to be able to accept JSON without using Object

+4
source share
3 answers

The first thing you need to do in asp.net mvc to publish data is to decorate this method with this [Httpost] attribute, which means passing a line should look like

 [HttpPost] public string Search(string a){ // your code } 

The default value is [HttpGet], which receives parameters from url. To send a request you need.

Edit:

And see the vinayan answer

with jquery:

 $.ajax({ method: "POST", url: "Home/Search", data: {'a': 'yourstring'} }) 
+4
source

As you understand, you want to transfer the entire request body to a string without binding, so that you can process the transferred string data using the method you need.

To direct this goal, simply write your own communication device:

 public class RawBodyBinder : IModelBinder { public object BindModel(ControllerContext controllerContext, ModelBindingContext bindingContext) { if(typeof(string)!=bindingContext.ModelType) return null; using (var s = new StreamReader(controllerContext.HttpContext.Request.InputStream)) { s.BaseStream.Position = 0; return s.ReadToEnd(); } } } 

And in your action method, assign your connecting element to the desired parameter:

 public string MyAction([ModelBinder(typeof(RawBodyBinder))]string json) { } 

But MVC has its own JSON mediator, and if your data is standard JSON, and in the request body with the header Content-Type: application/json you can use the MVC JSON mediator to activate the JSON mediator, just add the following line to the Global.asax.cs file Global.asax.cs :

 protected void Application_Start() { // some code here ValueProviderFactories.Factories.Add(new JsonValueProviderFactory()); } 
+4
source

The name of the parameter you are sending is used for de-serialization. So in this case, “a” should be part of json.

 public string Search(string a) 

so you have to use

 $.ajax({ method: "POST", url: "Home/Search", data: {'a': 'yourstring'} }) 
+1
source

All Articles