3

i'm trying to get my apicontroller to work. But somehow i cannot return Json().

Here's the error message from the compiler:

Error CS0029 Cannot implicitly convert type 'System.Web.Http.Results.JsonResult<>' to 'System.Web.Mvc.JsonResult' Opten.Polyglott.Web D:\Development\git\Opten.Polyglott\src\Opten.Polyglott.Web\Controllers\NewsletterApiController.cs

I cannot explain why it cannot convert the Json() to the ActionResult even the Json()inherits ActionResult.

Here's my controller:

using MailChimp;
using MailChimp.Helper;
using Opten.Polyglott.Web.Models;
using Opten.Umbraco.Common.Extensions;
using System.Configuration;
using System.Web.Mvc;
using Umbraco.Core.Logging;
using Umbraco.Web.WebApi;

namespace Opten.Polyglott.Web.Controllers
{
    public class NewsletterApiController : UmbracoApiController
    {
        public ActionResult Subscribe(Newsletter newsletter)
        {
            bool isSuccessful = false;
            if (ModelState.IsValid)
            {
                isSuccessful = SubscribeEmail(newsletter.Email);
            }

            return Json(new { isSuccess = isSuccessful });
        }
    }
}

Thanks for any help.

NinjaOnSafari
  • 998
  • 1
  • 8
  • 32
  • your method is return json object of a certain type but your return type is of jsonresult. i think it's expecting a type there – Baahubali May 10 '16 at 08:45
  • @user1490835 what is the difference between my code and this one: http://stackoverflow.com/questions/227624/asp-net-mvc-controller-actions-that-return-json-or-partial-html – NinjaOnSafari May 10 '16 at 08:48
  • System.Web.Mvc.JsonResult is the one you want. I'm not sure why "JSON" is mapping to System.Web.Http.Results.JsonResult. You can make it explicit: return System.Web.Mvc.Json(new { isSuccess = isSuccessful }); – CooncilWorker May 10 '16 at 09:11

4 Answers4

14

Your problem is within the usings as the UmbracoApiController most likely inherits from ApiController (from System.Web.Http) not Controller (from System.Web.Mvc) and thus they have different dependencies. To fix your problem first remove the

using System.Web.Mvc;

and put the

using System.Web.Http;

as for the return in this case that would be IHttpActionResult so you would have something as follows:

using MailChimp;
using MailChimp.Helper;
using Opten.Polyglott.Web.Models;
using Opten.Umbraco.Common.Extensions;
using System.Configuration;
using System.Web.Http;
using Umbraco.Core.Logging;
using Umbraco.Web.WebApi;

namespace Opten.Polyglott.Web.Controllers
{
    public class NewsletterApiController : UmbracoApiController
    {
        public IHttpActionResult Subscribe(Newsletter newsletter)
        {
            bool isSuccessful = false;
            if (ModelState.IsValid)
            {
                isSuccessful = SubscribeEmail(newsletter.Email);
            }

            return Json(new { isSuccess = isSuccessful });
        }
    }
}

Let me know if that works for you.

nizzik
  • 826
  • 5
  • 18
  • Your explanation about ApiController (not Controller) helped me understand why all my responses automatically became JSON, thanks! – RuudvK Oct 23 '18 at 21:06
8

It seems your Json is using class in System.Web.Http, not in System.Web.Mvc. In this case, you can use this code:

return new JsonResult{ isSuccess = isSuccessful };
nguyenhoai890
  • 1,189
  • 15
  • 20
1

When using ActionResult using Response.StatusCode is a good practice:

public ActionResult SomeMethod()
{
    try
    {
        // ...
        // doing something here...
        // ...

        // success:
        Response.StatusCode = (int)HttpStatusCode.OK;
        return Json(new { responseText = "OK" });
    }
    catch
    {
        // error:
        Response.StatusCode = (int)HttpStatusCode.BadRequest;
        return Json(new { responseText = "ERROR" });
    }
}
Jonathan Applebaum
  • 5,738
  • 4
  • 33
  • 52
0

Add the following line in your WebApiConfig.cs file:

config.Formatters.JsonFormatter.SupportedMediaTypes.Add(new MediaTypeHeaderValue("text/html"));
Chaos Legion
  • 2,730
  • 1
  • 15
  • 14