I want to create client side logging infrastructure using log4javascript. All logs are sent to server. That part works well. I have created WebApi controller to write those log messages on server. The request is the following:
I have created the following view models:
public class LogEntryViewModel
{
public string logger { get; set; }
public int timestamp { get; set; }
public string level { get; set; }
public string url { get; set; }
public string[] message { get; set; }
}
public class LoggerViewModel
{
public LogEntryViewModel data { get; set; }
public string layout { get; set; }
}
But the problem is that all data in LogEntryViewModel
is default. Even when I change public LogEntryViewModel data
to public JObject data
that results in empty data
object. I tried to parse Json that request contain and it is correct. What the problem might be here?
EDIT:
When I have changed Content-Type
to application/json;charset=UTF-8
The request looks in the following way:
And then I have changed Post method on controller:
// POST api/loggerservice
public void Post([FromBody]LogEntryViewModel log)
{
//BL
}
However, still the same effect.
In Web API, the media type determines how Web API serializes and deserializes the HTTP message body. There is built-in support for XML, JSON, and form-urlencoded data, and you can support additional media types by writing a media formatter.
I think that, based on your content-type (x-www-form-urlencoded), Web Api expects something like: logger=AjaxLogger×tamp=1375705087456...
so it can use the serializer for form-urlencoded data.
Since you're sending JSON to the server I would suggest to set the Content-Type
to application/json and use JSON.stringify
so it can use the JSON serializer for model binding:
data: JSON.stringify({'logger':'AjaxLogger', 'timestamp': '1234568789'})
You might want to remove the layout property as well now.
You don't need the [FromBody]
. This blogpost helped me to understand the [FromBody]
attribute
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments