How do you post a JSON file to an ASP.NET MVC Action?
I recently came up with a much simpler way to post a JSON, with the additional step of converting from a model in my app. Note that you have to make the model [JsonObject] for your controller to get the values and do the conversion.
Request:
var model = new MyModel();
using (var client = new HttpClient())
{
var uri = new Uri("XXXXXXXXX");
var json = new JavaScriptSerializer().Serialize(model);
var stringContent = new StringContent(json, Encoding.UTF8, "application/json");
var response = await Client.PutAsync(uri,stringContent).Result;
...
...
}
Model:
[JsonObject]
[Serializable]
public class MyModel
{
public Decimal Value { get; set; }
public string Project { get; set; }
public string FilePath { get; set; }
public string FileName { get; set; }
}
Server side:
[HttpPut]
public async Task<HttpResponseMessage> PutApi([FromBody]MyModel model)
{
...
...
}
You need to set the HTTP Header, accept, to 'application/json' so that MVC know that you as passing JSON and does the work to interpret it.
accept: application/json
see more info here: http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html
UPDATE: Working sample code using MVC3 and jQuery
Controller Code
namespace MvcApplication1.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
return View();
}
[HttpPost]
public JsonResult PostUser(UserModel data)
{
// test here!
Debug.Assert(data != null);
return Json(data);
}
}
public class UserModel
{
public int Id { get; set; }
public string FirstName { get; set; }
public string LastName { get; set; }
public int Age { get; set; }
}
}
View Code
@{ ViewBag.Title = "Index"; }
<script src="../../Scripts/jquery-1.9.1.min.js"></script>
<script type="text/javascript">
var sample = {};
sample.postData = function () {
$.ajax({
type: "POST", url: "@Url.Action("PostUser")",
success: function (data) { alert('data: ' + data); },
data: JSON.stringify({ "firstName": "Some Name", "lastName": "Some Last Name", "age": "30" }),
accept: 'application/json'
});
};
$(document).ready(function () {
sample.postData();
});
</script>
<h2>Index</h2>
** Update **
I added JSON.stringify
to the JS object before I pass it to the data
element in the AJAX Request. This just makes the payload more readable, however the Controller will interpret both formats of the data
similarly.