2

My Controller method is called successfully and the view model loads data, but throws an error upon returning.

public AccountManagerViewModel Get(string id)
{
    AccountManagerViewModel account = new AccountManagerViewModel(Guid.Parse(id));

    return account;
}

I tried adding the [Serializable] attribute to the class with no luck.

Does what I'm doing make sense? We're hoping to reuse as much code from our MVC app in the new Web API app so we really don't want to have to create new classes that we have to manually populate from our ViewModels and return from the Web API controller methods.

SomethingOn
  • 9,813
  • 17
  • 68
  • 107
  • "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'." – SomethingOn Aug 30 '15 at 18:05
  • How are you calling the `public AccountManagerViewModel Get(string id)`? Unrelated, instead of parsing the id, you can just make it `AccountManagerViewModel Get(Guid id)` and then instantiate it without parsing. – Ross Aug 30 '15 at 20:01
  • these links may help you , [link1](http://stackoverflow.com/questions/23098191/failed-to-serialize-the-response-in-web-api-with-json), [link2](http://stackoverflow.com/questions/14053109/failed-to-serialize-the-response-body-for-content-type), [link3](http://stackoverflow.com/questions/12641386/failed-to-serialize-the-response-in-web-api). and you better post your `AccountManagerViewModel ` here – J Santosh Aug 31 '15 at 02:12

2 Answers2

1

I know this is an old question, but hoping the answer will provide value for someone searching for the same thing...

I think that what you're really trying to do is return an HTTP response containing the serialized version of your ViewModel. If you change the return type of your method to IHttpActionResult then you can use built in WebAPI functions to return the result, including your automatically serialized ViewModel.

This would look something like the below.

public IHttpActionResult Get(string id)
{
    AccountManagerViewModel account = new AccountManagerViewModel(Guid.Parse(id));

    return Ok(account);
}

You can use other built in functions to return different HTTP responses too. e.g.

public IHttpActionResult Get(string id)
{
    if (string.IsNullOrWhiteSpace(id)) return BadRequest("Empty id parameter"); 

    AccountManagerViewModel account = new AccountManagerViewModel(Guid.Parse(id));

    if (account is null) return NotFound();

    return Ok(account);
}
Matt Shepherd
  • 782
  • 10
  • 21
0

You would really need to see what the error is. Maybe the GUID is incorrect? Incorrect format or just does not exit?

That should work just fine. Just one thing to keep in mind is to not serialise complex models, with recursion,etc. That may be another error that you may be getting. (But you can disable recursion serialisation too)

The best way to check what the error is, is to open the developer console in Chrome/ Firefox and Enable XMLHttpRequest logging. Then in the console you can click on the red response and view the error from ASP.NET.

enter image description here

Another way to check that the code is getting hit, is put a breakpoint on the first line of the code you expecting it to hit and run the Client. Then you can also step through the lines and see where the error is.

You can turn of deep serliastion for Entity Framework models.

var json = GlobalConfiguration.Configuration.Formatters.JsonFormatter;
json.SerializerSettings.PreserveReferencesHandling = 
    Newtonsoft.Json.PreserveReferencesHandling.None;
Piotr Kula
  • 9,597
  • 8
  • 59
  • 85
  • The Guid is ok. The ViewModel object is being populated correctly from the database. The error is definitely related to serialization. "The 'ObjectContent`1' type failed to serialize the response body for content type 'application/json; charset=utf-8'." – SomethingOn Aug 30 '15 at 18:05
  • Must be circular reference issue. Look for that error. You set something In Global. Is the model complex ?? – Piotr Kula Aug 30 '15 at 18:47
  • Ya we had an Entity Framework object in the ViewModel and some caching stuff, that broke serialization. Straight-up ViewModels can be returned no problem ;) – SomethingOn Aug 31 '15 at 20:47
  • Yea.. that is usually the case with directly serialising entity, there is allot of tracking stuff there, relationships etc.. that you may not want. Did you find the code to stop circular serliastion from entity? – Piotr Kula Aug 31 '15 at 20:49
  • I posted the line to do it. That should fix it for your EF serialisation. You can do that in Global.asax or in the controller. – Piotr Kula Aug 31 '15 at 20:50
  • just curious, if you change your method return type to match an IActionResult or JsonResult and manually convert the data, would the method work? https://stackoverflow.com/questions/44467409/trying-to-get-json-from-mvc-controller-and-getting-error – Bill Jun 15 '17 at 13:20