C# 如何修改 Web API 应用程序的默认允许响应大小设置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16436533/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 00:59:56  来源:igfitidea点击:

How to modify the default allowed response size settings for a Web API Application?

c#asp.netasp.net-mvciisasp.net-web-api

提问by The Light

I have a Web API method that returns a list of Events:

我有一个返回事件列表的 Web API 方法:

public HttpResponseMessage GetEvents()
{
...
}

My Service supports both Xml and JSON responses using DataContractSerializer (for xml) and DataContractJsonSerializer (for JSON).

我的服务支持使用 DataContractSerializer(用于 xml)和 DataContractJsonSerializer(用于 JSON)的 Xml 和 JSON 响应。

The response size might be like 30MB.

响应大小可能类似于 30MB。

What's the default allowed response size in ASP.NET Web API hosted in IIS?

IIS 中托管的 ASP.NET Web API 中允许的默认响应大小是多少?

How to modify the default settings?

如何修改默认设置?

What's the best practice in returning such large data (though it's not that large)?

返回如此大的数据(虽然不是那么大)的最佳实践是什么?

Should I zip the response?

我应该压缩响应吗?

Also, we may get one request per second.

此外,我们每秒可能会收到一个请求。

Thanks

谢谢

回答by Toan Vo

I am not sure about your problems. Because Responsehave not limit the size. We can limitation the response size by add more parameter Content-Lengthinto the response header. So I assume you will got two problems as below:

我不确定你的问题。因为Response没有限制大小。我们可以通过Content-Length在响应头中添加更多参数来限制响应大小。所以我假设你会遇到以下两个问题:

1. Request got limitation:To resolve it you should increase the request size to it can receive big size response. To increase request size you put into web.configas below:

1. 请求有限制:要解决这个问题,你应该增加请求的大小,让它可以接收大尺寸的响应。要增加请求大小,您输入web.config如下:

<system.web>
<httpRuntime maxRequestLength="2147483647" />

2. You got response buffer size are limitation exception:

2. 你得到响应缓冲区大小限制例外:

Please follow the linkfrom MSDN.

请按照MSDN 中的链接进行操作。

EDIT:

编辑:

What's the default allowed response size in ASP.NET Web API hosted in IIS?

IIS 中托管的 ASP.NET Web API 中允许的默认响应大小是多少?

The response size will auto get size by size of message we put into it. And it have limitation about response size. The HttpReponseMessageactually is a response similar I have posted above.

响应大小将根据我们放入其中的消息大小自动获取大小。并且它对响应大小有限制。该HttpReponseMessage实际上是类似我已经发布了上述回应。

What's the best practice in returning such large data (though it's not that large)?

返回如此大的数据(虽然不是那么大)的最佳实践是什么?

You should take link. The best practices to deal with data is convert to binary data and transfer it as many small-parts.

你应该拿链接。处理数据的最佳实践是将其转换为二进制数据并尽可能多地传输它。

Should I zip the response?

我应该压缩响应吗?

Depends on your context. IIS 7.0 already allow you configure zip response but take care on your code at client already support zip response or not.

取决于你的上下文。IIS 7.0 已经允许您配置 zip 响应,但请注意客户端上的代码是否已经支持 zip 响应。

回答by Shady Sherif

try this code in your web.config it solved my problem

在你的 web.config 中试试这个代码它解决了我的问题

    <configuration> 
       <system.web.extensions>
           <scripting>
               <webServices>
                   <jsonSerialization maxJsonLength="50000000"/>
               </webServices>
           </scripting>
       </system.web.extensions>
    </configuration>