Arghya C
Arghya C

Reputation: 10078

Json() method is documented for Web API but present only in MVC

I cannot find the Json() method in System.Web.Http.ApiController, though the method is present in System.Web.Mvc.Controller.

I have checked post Json method is not recognized in Web Api controller. I can see the same from the class definitions of Controller & ApiController.

BUT, in ApiController.Json Method MSDN documentation (no version mentioned), it says Json() is part of Api Controller/Web API.

What am I missing here? Why the Json() method is not present in ApiController as stated in the official docs! (I know, ApiController can still return JSON data when asked for.)

My System.Web.Http version is 4.0.0.0

Example:

using System;
using System.Web.Http;

namespace MyServices.Controllers
{
    public class TestController : ApiController
    {
        public object Get()
        {
            return Json("Dummy"); //<= doesn't compile
        }
    }
}

Upvotes: 0

Views: 390

Answers (1)

Craig W.
Craig W.

Reputation: 18165

I grabbed an older version of the Microsoft.AspNet.WebApi.Core nuget package (4.0.20505.0) in order to get the v4.0.0.0 version of the assembly. I then looked at the assembly with JustDecompile. ApiController does not have a Json method in that version. However in v5.2.3 of that package it does. The documentation you're referencing is for the newer version.

ApiController v4.0.0.0

ApiController v4.0.0.0

ApiController v5.2.3.0

ApiController v5.2.3.0

Upvotes: 2

Related Questions