MVC 空白 JsonResult

MVC Blank JsonResult

我正在返回一个 JsonResult 作为 Orchard CMS 站点 (MVC 4) 的一部分,它突然停止工作(具体如下)。

这是我的控制器的一个例子:

public JsonResult GetStartDate()
{
    //a previous version of this question was different here
    var startDate = _Settings.DateOpen.GetValueOrDefault(DateTime.MaxValue);
    if (startDate < DateTime.Now)
        startDate = DateTime.Now;


    var start = new
    {
        year = startDate.Time.Year,
        month = (startDate.Time.Month - 1)
    };

    return Json(start, JsonRequestBehavior.AllowGet);
}

它应该返回实际数据。我已验证该操作正在被命中,并且 "start" 在被传递到 Json 方法之前包含正确的数据。

如果我直接导​​航到 URL,我会看到空白屏幕。

检查AJAX请求的结果显示调用失败,响应主体为空字符串,状态为"parsererror",实际抛出的错误为"SyntaxError: Unexpected end of input."

POST 请求出于某种原因工作,此问题仅适用于 GET。

此问题与 所有 JSON GET 有关,而不仅仅是任何特定方法。

什么会影响 Json 方法和接收响应的客户端之间的数据?

默认情况下缓存所有 MVC 方法。这可能会导致 change/debug 循环期间出现问题。对于初学者,您可以在开发期间使用下面的装饰器关闭缓存。

[OutputCache(NoStore = true, Duration = 0)] 
public ActionResult MyMethod()

您可能希望使用 web.config 中定义的缓存方案包装所有方法。这允许通过一个在开发过程中派上用场的配置更改来控制缓存。

 <system.web>
    <caching>
      <outputCacheSettings>
        <outputCacheProfiles>
          <clear />
          <add varyByParam="*" duration="0" name="MyCachePlan" />          
        </outputCacheProfiles>
      </outputCacheSettings>
    </caching>
 </system.web>

并使用以下装饰器:

[OutputCache(CacheProfile = "MyCachePlan")]
public ActionResult MyMethod()