MVC FilePathResult操作会在Chrome中导致重复请求

时间:2016-08-03 14:29:10

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

我有一个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

1 个答案:

答案 0 :(得分:0)

尝试使用以下内容添加Content-Disposition标头到您的操作中的响应"附件;文件名=检验.pdf"或者只是"附件"

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