1

The following is an example how i am posting data to a controller action in MVC.The data is perfectly populating to my model object and working fine.

var data={};
data.SearchText = 'abc'
data.SearchText1 = 'abcd'
var contentType = 'application/json; charset=utf-8';

$.ajax({
      type: 'POST',
      cache: false,
      contentType: contentType,
      url: User/_UserList,
      data: JSON.stringify(data),
      success: successHandlerFunction,
      complete: completeHandlerFunction
});

[HttpPost]
public JsonResult _UserList(SearchViewModel model)
{
    var  users= "" ; //Get Data from DB ;
    return Json(users, JsonRequestBehavior.AllowGet);
}

But in case of an exception i am trying to fetch value of model

public void OnException(ExceptionContext filterContext)
{


//filterContext.HttpContext.Request.Form -- is not giving any value
//filterContext.HttpContext.Request.QueryString -- is also having no value
//filterContext.HttpContext.Request.Params -- also no value about model

}

Can anybody give me a clue why the above piece of code not working

adiga
  • 34,372
  • 9
  • 61
  • 83
shu
  • 1,938
  • 10
  • 19

2 Answers2

2

Sorry - can't comment your answer, so placing a new answer. The solution is right - you should make the contentType as 'application/x-www-form-urlencoded'. The reason is clearly described here: HttpRequest.Form Property

The Form property is populated when the HTTP request Content-Type value is either "application/x-www-form-urlencoded" or "multipart/form-data".

As for 'JSON.stringify(data),' - that will make one string from the dictionary actually and in this case HttpRequest.Form cannot be populated as it requires a dictionary, basically, just 'data'.

drcolombo
  • 194
  • 6
  • Yeah I got it.But just wanted to know if we post data with contentType 'application/json; charset=utf-8' ,Then How one can get the posted data from the Request object or is there any other way to retrieve the posted data – shu Jan 12 '16 at 04:15
  • I tried the following function to extract the JSON Data still I am not able to fetch the json data from request object. private string GetJsonContents(System.Web.HttpRequestBase Request) { string JsonContents; using (Stream receiveStream = Request.InputStream) { using (StreamReader readStream = new StreamReader(receiveStream)) { JsonContents = readStream.ReadToEnd(); } } return JsonContents; } – shu Jan 12 '16 at 04:56
  • But why do you need to extract Json data from a raw HttpRequest? Especially, if we're talking about ASP.NET MVC. Maybe it will be better to use something like [this](http://stackoverflow.com/questions/1302946/asp-net-mvc-controlling-serialization-of-property-names-with-jsonresult)? – drcolombo Jan 12 '16 at 19:39
0

By Changing the contentType from 'application/json; charset=utf-8' to 'application/x-www-form-urlencoded' the HttpContext.Request.Form started populating the value.We also need to remove the JSON.stringify while passing the data.

If we post the data with contentType 'application/json; charset=utf-8' .And from the request object we need to retrieve the Json Contents then we can use the following piece of code

 private string GetJsonContents(System.Web.HttpRequestBase Request)
    {
        string JsonContents = string.Empty;
        try
        {
            using (Stream receiveStream = Request.InputStream)
            {
                using (StreamReader readStream = new StreamReader(receiveStream))
                {
                    receiveStream.Seek(0, System.IO.SeekOrigin.Begin);
                    JsonContents = readStream.ReadToEnd();
                }
            }
        }
        catch
        {
        }
        return JsonContents;
    }

Then we can use the following code to get the JSON Object

 string requestData = GetJsonContents(HttpContext.Current.Request);
 dynamic jsonObject = JsonConvert.DeserializeObject<dynamic>(requestData);//using Newtonsoft dll
shu
  • 1,938
  • 10
  • 19