3

I am building a textbook .NET WebAPI web service, which is supposed in the most simplistic case receive a GET request and return a JSON array of objects.

But the project also loads NewtonsoftJSON nuget package, and apparently that package re-defines the ApiController class, so that its Json() method has a different declaration. The end result is that I get the following compile time error:

enter image description here

How can I protect the declaration of my controller from being re-defined by Newtonsoft?

public class ImmediateInformationController : ApiController
{
    public JsonResult Get([FromUri] ImmediateInformation ii)
    {
        // some code here
        return this.Json(iil, JsonRequestBehavior.AllowGet);
    }
}

A bit of a background: initially I was returning just iil, but when testing in the browser, it prompted to download ImmediateInformation.json file, which contained the JSON I needed. The above was an attempt to have the JSON returned as plain text, and that lead me to the discovery that Newtonsoft re-defined ApiController when parameter types did not match MSDN documentation.

enter image description here

Just to re-cap: the question is only using JSON download issue to illustrate what I was doing. How can I ensure that when I am declaring the controller class to be a descendant of ApiController, it is Microsoft's API controller, and not Newtonsoft's?

ajeh
  • 2,652
  • 2
  • 34
  • 65
  • 2
    You seem to be mixing up MVC and WebAPI namespaces/code... [JsonRequestBehavior](https://msdn.microsoft.com/en-us/library/system.web.mvc.jsonrequestbehavior%28v=vs.118%29.aspx) is ASP.Net MVC enum and should not be used in WebAPI code (same for attributes that have same names in two namespaces). Posting more complete [MCVE] with namespaces would make it clear... – Alexei Levenkov Feb 13 '18 at 21:53
  • 1
    Yeah, I realized that since. My problem is that I only know of WebAPI for about 2 hours, so making some silly mistakes, and the references like Newtonsoft re-defining MVC classes confused be beyond belief. – ajeh Feb 13 '18 at 21:56

2 Answers2

5

Web API 2 includes JSON.NET by default, and uses that in its Json() method. Your tutorial probably doesn't target Web API 2, but an earlier version, or MVC instead of Web API.

Either omit the JsonRequestBehavior:

return this.Json(iil);

Or simply return the proper return type and let Web API handle the serialization:

public ImmediateInformation Get([FromUri] ImmediateInformation ii)
{
    return ii;
}

Which you could also do using an IHttpActionResult:

public IHttpActionResult Get([FromUri] ImmediateInformation ii)
{
    return this.Ok(ii);
}

According to your edit and comment, you're actually looking for How can I convince IE to simply display application/json rather than offer to download it?.

CodeCaster
  • 147,647
  • 23
  • 218
  • 272
  • I've initially returned POCO, but was confused when the browser popped up a download prompt for the file called `.json` and started looking for a way to simply return the result in the body of the response. This probably lead me on a wild goose chase above. Sounds like something is wrong with the default content disposition. – ajeh Feb 13 '18 at 21:51
  • I guess you use Internet Explorer, which doesn't like rendering JSON. – CodeCaster Feb 14 '18 at 08:23
  • If I returned the same JSON from .ashx, it would be displayed. I asked another question for this issue here https://stackoverflow.com/q/48777442/2721750 – ajeh Feb 14 '18 at 14:52
0

You'll need to fully qualify the parameter.

return this.Json(iil, System.Web.Mvc.JsonRequestBehavior.AllowGet);

Or you can use an alias.

using NJson = Newtonsoft.Json;
Drewness
  • 5,004
  • 4
  • 32
  • 50
  • I am not `using` Newtonsoft, it is used by one of the referenced nuget packages (MigraDocs). – ajeh Feb 13 '18 at 21:49
  • Ah, so alias is out then. – Drewness Feb 13 '18 at 21:51
  • Fully qualifying is also out, as it's the `ApiController` class which is re-defined by Newtonsoft (or its method `Json`), but the `JsonRequestBehaviour` is fine. – ajeh Feb 13 '18 at 21:54
  • I see, and your updates to the question make that clear now. I would go with CodeCaster's suggestions then. – Drewness Feb 13 '18 at 21:57