簡體   English   中英

ASP.NET web 服務413請求實體太大錯誤

[英]ASP.NET web service 413 request entity too large error

I have an older ASP.NET API web service using .net 4.5.2 and I am posting an object that contains a base64 image to my controller without any problem. 當我嘗試使用更多和更大的圖像發布數據並且我收到 413 請求實體太大錯誤時,問題就出現了。 我一直在查找東西並嘗試了我在網上找到的所有東西,但沒有運氣。 我正在尋找上傳大小約為 10MB 的文件。 讓我相信它與服務器相關的一件事是在 IIS Express 下運行服務時,我可以在本地上傳大文件。

我嘗試將 MaxRequestLength 和 MaxAllowableContentLength 添加到 web 配置中。

<system.web>
    <!-- tell IIS to let large requests through -->
    <httpRuntime maxRequestLength="52428800" />

<system.webServer>
    <security>
        <requestFiltering>
            <requestLimits maxAllowedContentLength="52428800" />
        </requestFiltering>
    </security>

我還在 IIS v6.2 中對我的 windows 2012 R2 服務器進行了更改,以允許更大的文件。 我還調整了服務器上的 UploadReadAhead 值。

在此處輸入圖像描述

在此處輸入圖像描述

在此處輸入圖像描述

我在 API 配置 class 中沒有任何特別之處。

public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.MapHttpAttributeRoutes();

        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

        var appXmlType = config.Formatters.XmlFormatter.SupportedMediaTypes.FirstOrDefault(t => t.MediaType == "application/xml");
        config.Formatters.XmlFormatter.SupportedMediaTypes.Remove(appXmlType);
    }
}

我的配置中用於此服務的綁定

    <webHttpBinding>
        <binding name="SecureServerBindingWithUserNameRest"  maxReceivedMessageSize="52428800">
            <readerQuotas maxArrayLength="10485760" />
                <security mode="TransportCredentialOnly">
                <transport clientCredentialType="Basic" proxyCredentialType="None" />
            </security>
        </binding>
        <binding name="webHttpTransportSecurityRest">
            <security mode="Transport" />
        </binding>
    </webHttpBinding>

你可以試試這個解決方案,嘗試增加上傳大小限制。 IIS 在 applicationHost.config 和 web.config 文件中使用 uploadReadAheadSize 參數。

  1. 打開 IIS 管理器
  2. Select 網站
  3. 雙擊“配置編輯器”
  4. Select system.webServer然后serverRuntime
  5. 修改uploadReadAheadSize值為2147483647
  6. 點擊“申請”

您也可以考慮在發布大型內容之前執行一個虛擬的 get 請求。 初始化 https 連接時,此第一個請求不應很大。 一旦 https 連接被初始化,繼續請求不受限制。

當我在發送包含大量 PDFS 的壓縮文件時遇到此錯誤時,我發現的解決方案是除了 maxRequestLength 之外,還將添加到 web 配置 requestLengthDiskThreshold 中:

<system.web>
    <httpRuntime maxRequestLength="268435456" requestLengthDiskThreshold="268435456" executionTimeout="600"/>

我還添加了比 90 預設更長的超時,因此它有時間發送文件。 請記住,requestLengthDiskThreshold 不得超過 maxRequestLength。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM