我可以找到最大POST大小是否太大?

时间:2014-11-24 15:49:26

标签: c# asp.net-web-api

我通过POST将文件发送到我的ApiController。

如果文件低于2 MB,一切都像魅力一样。 如果文件较大,我会收到错误404。

这是我的Controller中的(旧)函数声明:

 [HttpPost]
 public HttpResponseMessage FileUpload(HttpRequestMessage req, string entryId = "", string owner = "", int debug = 0)
 {

如果实体太大,则返回:

Remote Address:172.17.41.12:443
Request URL:https://webdevserver/myapp/api/Debug/FileUpload
Request Method:POST
Status Code:404 Not Found

或者如果它在尺寸限制范围内,则:

Remote Address:172.17.41.12:443
Request URL:https://webdevserver/myapp/api/Debug/FileUpload
Request Method:POST
Status Code:200 OK

所以我想发送一个有用的错误消息 - 错误404肯定不是! - 并偶然发现HTTP状态码413,IIS没有自动发送:(所以我将我的代码更改为:

[HttpPost]
public HttpResponseMessage FileUpload(HttpRequestMessage req=null, string entryId = "", string owner = "", int debug = 0)
{
    if(req==null) {
        // POST was not handed over to my function by IIS
        // now is it possible to check whether file was empty or too large!? Because
        return new HttpResponseMessage(HttpStatusCode.RequestEntityTooLarge);
        // should only be sent if POST content was really too large!

那么,如何检查POST数据的大小是否太大或POST是否为空?

1 个答案:

答案 0 :(得分:1)

根据此blog,IIS 7中引入了状态代码404.13,以替换http状态代码413

由于这是通过设计完成的,我建议您按原样维护响应,并在您的代码中尝试确定404错误是否实际上是404.13错误。