Inconsistent property name casing in generated JsonResult

时光怂恿深爱的人放手 提交于 2019-12-12 16:17:49

问题


I've been scratching my head on this for a while now and have decided to let the SO community take a crack at it...

I have a few actions that respond to POSTs from the client side that do simple tasks and return a JsonResult built from an anonymous class with a simple Boolean "Success" property if it succeeds, or returns a PartialViewResult with ModelState errors if the action was unsuccessful:

<HttpPost()> _
Public Function UpdateHeader(ByVal header As XmlReturnHeader) As ActionResult
    If ModelState.IsValid Then
        Dim updated As Integer = TaxRepository.XmlReturnHeader.Update(header)
        If updated = 1 Then
            Return Json(New With {.Success = True}, JsonRequestBehavior.AllowGet)
        End If
    End If
    Return PartialView("Maintenance/Header", header)
End Function

On the client side, the code is equally as simple. I want to see if the result has a Success property, make sure that it was really true and then go from there:

$.post('<%= Url.Action("UpdateHeader") %>', $(this).serialize(), function (data) {
    if (data.Success && data.Success === true) {
        $('#list').trigger('reloadGrid');
        $('#edit').dialog('close');
    } else { // result must be the HTML
        $('#edit').html(data);
    }
});

However, I ran into issues testing. The data object I receive from the callback function always looks like this:

{ success: true }
  ^

The s in success is lower-case, and because JS is case-sensitive, I have a problem.

The strange part is if I change the VB.NET code to

Return Json(New With {.Garbage = True}, JsonRequestBehavior.AllowGet),

I will receive

{ Garbage: true } 

If I change it back to New With { .Success = True }, I again see the lower-case s.

I am using IIS and IE9 for testing. I feel like the response is somehow being cached. There are other places in the application where I use a lower-case "success" as a property in an anonymous JsonResult parameter. Perhaps IIS is caching a response from a previous GET or POST request?

[Update]

To test the response caching theory, I changed my code to:

Return Json(New With {.Success = True, .Random = DateTime.Now.Ticks},
   JsonRequestBehavior.AllowGet)

This caused the correct response to be received on the client side. Great. But what the heck was going on before? I thought POSTs were exempt from caching? Or is that only requests and not Responses?

How can I avoid this in the future?

[Update]

One other tidbit: I do globally disable caching my requests via jQuery, but I do not have any OutputCacheAttributes or any such "no-cache" mechanism employed on the server side. I quicky slapped <OutputCache(NoStore:=True, Duration:=0, VaryByParam:="*")> on the entire controller, removed the .Random property from the JSON, but still received { success: true }.

I have go so far as to recycle application pools, remove Temporary .NET Files, and clear browser cache, and the same problem persists.


回答1:


If you haven't seen it, you may want to take a look at the following article on Output Caching in ASP.net MVC: http://juristr.com/blog/2012/10/output-caching-in-aspnet-mvc/

According to the article, the default response headers include:

Cache-Control:private

Which means that IE can cache the response in a private cache. The article offers three options for disabling caching of these requests: disabling it globally on the server (which I don't recommend for client performance reasons), writing a custom global action filter (which has the same result), or adding an attribute to your controller action called OutputCache:

<OutputCache(Duration:=0)>

While the jQuery setting should perform a similar function, it may not garner the same result. Try this and see if it works.



来源:https://stackoverflow.com/questions/12961607/inconsistent-property-name-casing-in-generated-jsonresult

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!