MVC OutputCache JsonResult returns html

时间:2015-11-12 10:55:33

标签: ajax caching model-view-controller

I have the following controller:

[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);
    }

which is called by the following ajax function

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.");
        }
    });

This code works fine without the OutputCache attribute on the controller. With it it always hits the error function of the ajax call and I see that the response is not JSON but HTML content (error is a parser error therefore). What could be going wrong with the outputcaching and how do I get it working correctly? I've tried many ways of supplying VaryByParams but they all have the same result.

1 个答案:

答案 0 :(得分:0)

This post revealed the answer. The problem was outputting the trace to the page: page outputcache not working with trace