不一致的属性名在外壳产生JsonResult [英] Inconsistent property name casing in generated JsonResult
问题描述
我一直在抓我的头在这一段时间,并已决定让SO社会采取裂纹吧...
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...
我有一个从客户端是做简单的任务,并返回回复帖子的几个动作 JsonResult
从一个匿名类建有一个简单的布尔成功的属性如果成功,或返回 PartialViewResult
与的ModelState
错误,如果行动失败:
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
在客户端中,code是同样简单。我想看看结果有一个成功
属性,确保它是真的真正
键,然后再从那里
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 }
^
的取值
在成功
是小写的,因为JS是区分大小写的,我有一个问题
The s
in success
is lower-case, and because JS is case-sensitive, I have a problem.
奇怪的部分是,如果我改变VB.NET code到
The strange part is if I change the VB.NET code to
Return Json(New With {.Garbage = True}, JsonRequestBehavior.AllowGet),
我将获得
{ Garbage: true }
如果我改回新增功能{.Success = TRUE}
,我再次看到了小写取值
。
If I change it back to New With { .Success = True }
, I again see the lower-case s
.
我使用IIS和IE9测试。我觉得自己的反应是莫名其妙地被缓存。有应用程序的其他地方,我用的是小写的成功作为属性在一个匿名 JsonResult
参数。也许是IIS缓存从previous响应GET或POST请求?
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?
[更新]
要测试的响应缓存理论,我改变了我的code为:
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?
[更新]
另外一个珍闻:我不关闭全局通过jQuery缓存我的要求,但我没有任何 OutputCacheAttribute
s或对雇用任何这样的无缓存机制服务器端。我quicky耳光&LT;的OutputCache(NoStore:= TRUE,持续时间:= 0,VaryByParam时:=*)&GT;
整个控制器上,去掉了 .Random
从JSON财产,但仍然收到 {成功:真正}
。
One other tidbit: I do globally disable caching my requests via jQuery, but I do not have any OutputCacheAttribute
s 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 }
.
我已经走得很远,回收应用程序池,删除临时文件.NET和清除浏览器缓存,同样的问题仍然存在。
I have go so far as to recycle application pools, remove Temporary .NET Files, and clear browser cache, and the same problem persists.
推荐答案
如果您还没有看到它,你可能要看一看在输出缓存下面的文章在ASP.net MVC:的 http://juristr.com/blog/2012/10/output-caching-in -aspnet-MVC /
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
这意味着IE的可以的缓存在私有缓存的响应。本文提供了三个选项禁用缓存这些请求:禁止全局在服务器上(我不建议客户端性能的原因),编写定制的全球行动过滤器(具有相同效果),或添加属性叫你的控制器动作的OutputCache:
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)>
虽然jQuery的设置应执行类似的功能,它可能无法争取同样的结果。试试这个,看看它是否工作。
While the jQuery setting should perform a similar function, it may not garner the same result. Try this and see if it works.
这篇关于不一致的属性名在外壳产生JsonResult的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!