MVC 4返回JSON作为ActionResult [英] MVC 4 return JSON as ActionResult
问题描述
我正在尝试让我的apicontroller工作.但是以某种方式我无法返回Json()
.
i'm trying to get my apicontroller to work. But somehow i cannot return Json()
.
以下是来自编译器的错误消息:
错误CS0029无法隐式转换类型 'System.Web.Http.Results.JsonResult<>' 到 'System.Web.Mvc.JsonResult'Opten.Polyglott.Web D:\ Development \ git \ Opten.Polyglott \ src \ Opten.Polyglott.Web \ Controllers \ NewsletterApiController.cs
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
我无法解释为什么即使Json()
继承了ActionResult
,它也不能将Json()
转换为ActionResult
.
I cannot explain why it cannot convert the Json()
to the ActionResult
even the Json()
inherits ActionResult
.
这是我的控制人:
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 });
}
}
}
感谢您的帮助.
推荐答案
您的问题在用法之内,因为UmbracoApiController
很可能是从ApiController (from System.Web.Http)
而不是Controller (from System.Web.Mvc)
继承的,因此它们具有不同的依赖性.要解决您的问题,请先删除
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;
然后放
using System.Web.Http;
在这种情况下的回报为IHttpActionResult
,因此您将获得以下信息:
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.
这篇关于MVC 4返回JSON作为ActionResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!