Reputation: 2902
I have a home controller method below which is invoked from a http post request. I need to read the request data and send it to the view. Is there a way to look at the raw data without creating parameters in the SaveResponse() ? Thanks for any suggestions.
public ActionResult SaveResponse()
{
//Read the http post request body as json and send it to view
//HttpContext.Request.RequestContext
return View("CallbackView");
}
The request body will be in JSON format.
{
"customers":
{
"firstName": "Test”,
"lastName": “Lastname”,
"fullAddress":
{
"streetAddress": "123 springs lane",
"city": "New York",
"state": "NY",
"postalCode": 10021
}
}
}
Upvotes: 0
Views: 2000
Reputation: 9642
You can do it by reading Request.InputStream
[HttpPost]
public ActionResult SaveResponse()
{
string json;
using (var reader = new StreamReader(HttpContext.Request.InputStream))
{
json = reader.ReadToEnd();
}
return View("CallbackView");
}
or you can accept string
parameter and model binder will do everything for you, but you need to pass data as a string via json
parameter. Some of available options
Set Content-Type: application/json
and payload like {"json": "{id:44}"}
Set Content-Type: x-www-form-urlencoded
and payload like json={id:44}
Action
[HttpPost]
public ActionResult SaveResponse(string json)
{
return View("CallbackView");
}
Upvotes: 2