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.
Add the following line in your WebApiConfig.cs file:
Your problem is within the usings as the
UmbracoApiController
most likely inherits fromApiController (from System.Web.Http)
notController (from System.Web.Mvc)
and thus they have different dependencies. To fix your problem first remove theusing 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:Let me know if that works for you.
It seems your Json is using class in System.Web.Http, not in System.Web.Mvc. In this case, you can use this code: