创建后演示项目 http://www.ok-soft-gmbh.com/ForStackOverflow/ProductStore.zip它可以用来重现“问题”我可以检查和描述它。
To reproduce the problem one need to start the MVC application and to use Firefox as frontend. One should start the integrated Debugger (by Ctrl+Shift+S or menu "Tools" / "Web Developer" / "Debugger") and to examine Browser Console window. The window contains many warnings, which are suspected for Firefox, but what are absolutely correct actions and the warnings are absolutely unneeded. After deleting of any row one will see the message like
我仔细检查了这个问题,确实如此错误警告,因为对 REST 操作的 HTTP 流量的误解。 ASP.NET MVC 的 DELETE 方法,其中有void
作为返回值(例如public void DeleteProduct(int id)
) 产生如下 HTTP 响应
HTTP/1.1 204 No Content
Cache-Control: no-cache
Pragma: no-cache
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNA==?=
X-Powered-By: ASP.NET
Date: Fri, 12 Feb 2016 09:23:51 GMT
火狐浏览器的bug:它为所有没有正文的 HTTP 响应显示消息“未找到元素”。因此如果状态码是204
(无内容)或者如果状态代码是200
(OK),但是body是空的(存在HTTP headerContent-Length: 0
)然后 Firefox 怀疑未找到 REST 资源,并显示“警告”和文本“未找到元素”。
如果您不想看到该消息,则必须在 DELETE 响应正文中返回一些数据。例如
public HttpResponseMessage DeleteProduct(int id)
{
bool isDeleted = _repository.Remove(id);
if (!isDeleted) {
throw new HttpResponseException(HttpStatusCode.NotFound);
}
return Request.CreateResponse(HttpStatusCode.OK, "OK!");
}
产生的响应如下
HTTP/1.1 200 OK
Cache-Control: no-cache
Pragma: no-cache
Content-Type: application/json; charset=utf-8
Expires: -1
Server: Microsoft-IIS/10.0
X-AspNet-Version: 4.0.30319
X-SourceFiles: =?UTF-8?B?QzpcVXNlcnNcT2xlZ1xEb3dubG9hZHNcUHJvZHVjdFN0b3JlXFByb2R1Y3RTdG9yZVxhcGlccHJvZHVjdHNcNg==?=
X-Powered-By: ASP.NET
Date: Fri, 12 Feb 2016 09:05:19 GMT
Content-Length: 5
"OK!"
我个人认为,最好忽略 Firefox 的“警告”并持有public HttpResponseMessage DeleteProduct(int id)
。我仍然建议您更新您使用的存储库
interface IProductRepository
{
IEnumerable<Product> GetAll();
Product Get(int id);
Product Add(Product item);
bool Remove(int id);
bool Update(Product item);
}
where Remove
将布尔值作为返回类型。实施可以是
public bool Remove(int id)
{
return _products.RemoveAll(p => p.Id == id) > 0;
}
和 MVC 代码
public void DeleteProduct(int id)
{
_repository.Remove(id);
}
将被固定为
public void DeleteProduct(int id)
{
bool isDeleted = _repository.Remove(id);
if (!isDeleted)
{
throw new HttpResponseException(HttpStatusCode.NotFound);
}
}
我想强调的是,上述所有问题都是纯粹的 ASP.NET MVC 问题或 Firefox 问题,与免费的 jqGrid 或 jqGrid 没有直接关系。
可以下载修改后的项目here http://www.ok-soft-gmbh.com/ForStackOverflow/ProductStore1.zip. The ProductsController.cs
文件包含注释版本DeleteProduct
,这在 Firefox 中不会产生任何警告。您可以通过更改虚拟文本来使用代码"OK!"
到空字符串""
或者一些其他的测试。 Firefox 的 bug 非常古老(它的起源是错误 521301 https://bugzilla.mozilla.org/show_bug.cgi?id=521301).