2015-11-12 97 views
0

我有以下控制器:MVC的OutputCache JsonResult返回HTML

[HttpPost] 
    [OutputCache(Duration=3600, VaryByParam="*", Location=OutputCacheLocation.Server)] 
    public JsonResult FreeTextQuery(SearchFiltersQuery filters) 
    { 
     Trace.TraceInformation("Entering method SearchController.FreeTextQuery"); 

     SearchResults aResults = new SearchResults(); 

     if (ModelState.IsValid) 
     { 

      try 
      { 
       ClaimsPrincipal user = User as ClaimsPrincipal; 
       aResults = _objectRepository.GetFullTextResults(filters, user); 
      } 
      catch (Exception ex) 
      { 
       if (!(@Url == null)) 
       { 
        return Json(new { redirectUrl = @Url.Action("ShowError", "Error", new { message = ex.Message }), isRedirect = true }); 
       } 
      } 
     } 

     Trace.TraceInformation("Exiting method SearchController.FreeTextQuery"); 

     return Json(aResults); 
    } 

這是由以下AJAX功能稱爲

function GetResults(aFilters) { 
    var aEndPointUrl = "/Search/FreeTextQuery"; 

    var jSonString = JSON.stringify(aFilters); 
    $.ajax({ 
     type: 'POST', 
     url: aEndPointUrl, 
     traditional: true, 
     contentType: 'application/json; charset=utf-8', 
     data: jSonString, 
     success: function (data) { 
      // omitted for brevity 

     }, 
     error: function (xhr, ajaxOptions, error) { 
      window.location.href = "/Error/ShowError?message=" + encodeURIComponent("Onbekende fout bij het zoeken."); 
     } 
    }); 

此代碼工作正常,而不在控制器上的OutputCache屬性。有了它,它總是碰到ajax調用的錯誤函數,我發現響應不是JSON,而是HTML內容(因此錯誤是解析器錯誤)。 輸出緩存有什麼問題,如何讓它正常工作?我嘗試了很多提供VaryByParams的方法,但它們都有相同的結果。

回答