【问题标题】:MVC FilePathResult Action causes repeat request in ChromeMVC FilePathResult Action 导致 Chrome 中的重复请求
【发布时间】:2016-12-09 08:17:26
【问题描述】:

我有一个如下调用的 MVC 操作:

http://localhost/Report/PrintContainerMasterTrayList?containerMasterId=x&type=downloadview

该操作具有以下签名

public ActionResult PrintContainerInstanceTrayList(int containerInstanceId, string type)

返回 FilePathResult 如下:

return File(filename, "application/pdf");

这一切都很好,只是在 Chrome 中请求会触发两次。该请求仅在 Firefox 和 IE11 中发出一次。

原始请求

GET http://localhost/DEV-SAF/Report/PrintContainerMasterTrayList?containerMasterId=x&type=downloadview HTTP/1.1 Host: localhost Connection: keep-alive Cache-Control: max-age=0 Upgrade-Insecure-Requests: 1 User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8 Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en;q=0.8 Cookie: .ASPXANONYMOUS=-

第二个请求

GET http://localhost/DEV-SAF/Report/PrintContainerMasterTrayList?containerMasterId=x&type=downloadview HTTP/1.1 Host: localhost Connection: keep-alive User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/52.0.2743.82 Safari/537.36 Accept: */* Referer: http://localhost/DEV-SAF/Report/PrintContainerMasterTrayList?containerMasterId=x&type=downloadview Accept-Encoding: gzip, deflate, sdch Accept-Language: en-GB,en;q=0.8

两个请求的响应是相同的(在这种情况下,一个 4Mb 的文件占用了过多的带宽)。

是什么导致了这种行为?

编辑

第一个请求的响应是 HTTP/1.1 200 OK Cache-Control: private, s-maxage=0 Content-Type: application/pdf Server: Microsoft-IIS/10.0 Set-Cookie: .AUTH=xxxxxx; path=/; HttpOnly X-AspNetMvc-Version: 5.2 Date: Wed, 03 Aug 2016 14:17:41 GMT Content-Length: 3999

第二个请求的响应是 HTTP/1.1 200 OK Cache-Control: private, s-maxage=0 Content-Type: application/pdf Server: Microsoft-IIS/10.0 X-AspNetMvc-Version: 5.2 Date: Wed, 03 Aug 2016 14:17:46 GMT Content-Length: 3999

【问题讨论】:

  • 你粘贴的请求不一样,例如比较查询字符串中的containerMasterId=94101和containerMasterId=x
  • 对不起,是我复制和粘贴的。编辑了帖子。
  • 这两个请求是一起触发还是一个一个触发?
  • 第二个请求在收到第一个响应后被触发。
  • 我已经添加了回复。它们的不同之处在于第一个包含一个 cookie。

标签: c# asp.net-mvc google-chrome httprequest http-get


【解决方案1】:

尝试在您的操作中添加 Content-Disposition 标头以响应以下内容“附件;文件名=test.pdf”或简单的“附件”

Response.AppendHeader("Content-Disposition", "attachment");

【讨论】:

猜你喜欢
  • 2014-06-09
  • 2014-09-11
  • 1970-01-01
  • 1970-01-01
  • 2022-12-20
  • 1970-01-01
  • 2021-12-14
  • 1970-01-01
  • 2020-04-23
相关资源
最近更新 更多