Increase max url length in asp.net core

安稳与你 提交于 2019-12-01 08:54:28

问题


I have an asp.net core web api, that enables tts requests. You basically request http://server.url/Whatever you want it to say.mp3 to get an mp3 with the text spoken in tts.

But the problem is, that it will return a 400 - The request URL is invalid if I pass it a long string. It seems like the server is hitting some sort of max URL length limitation. I can find information on how to fix this is the "classic" asp.net web.config, but not how to fix this in asp.net core.

What is the correct way of increasing the maximum URL length in asp.net core?

Update:

The exact url is for instance:

http://localhost:5000/api/mp3/nl-NL/male/1/Wikipedia%20is%20een%20online%20encyclopedie%20die%20tracht%20inhoud%20te%20bieden%20die%20vrij%20herbruikbaar,%20objectief%20en%20verifieerbaar%20is.%20Het%20project%20is%20gebouwd%20op%20vijf%20zuilen.%20De%20Nederlandstalige%20versie%20startte%20op%2019%20juni%202001%20en%20is,%20gemeten%20naar%20het%20aantal%20artikelen,%20met%20bijna%201,9%20miljoen%20artikelen%20de%20op%20vier%20na%20grootste%20taalversie.mp3

It is URL-encoded. I'll try to figure out what the exact maximum is.

I cannot use a POST, as the tool that is going to make use of this only supports urls for mp3 files (hence the .mp3 extension used in my controller).

Update 2

The longest request it accepts has 397 characters, which does not seem very logical. If I add one more, it will return a code 400.

Update 3

It turns out to only have this issue when deployed on the server (using IIS), when run locally, it supports way longer strings (I haven't seen it return a code 400 yet). I'll have to look into IIS to see where it goes wrong.


回答1:


As it turns out, I needed two things to fix this:

Change web.config

I needed to add the following to the web.config to instruct IIS to allow longer URLs:

<system.webServer>
    <security>
        <requestFiltering allowDoubleEscaping="false">
            <requestLimits maxAllowedContentLength="30000000" maxUrl="40960" maxQueryString="20480" />
        </requestFiltering>
    </security>
</system.webServer>

Edit registry

After this, IIS will still throw a code 400, because the UrlSegment length is too long. Sadly, the only way to change this is by some minor registry hacking.

Navigate to HKEY_LOCAL_MACHINE\SYSTEM\CurrentControlSet\Services\HTTP\Parameters and find the key UrlSegmentMaxLength. If it is not there, create one with a value type of REG_DWORD. I used 1000 HEX (4096 DEC) as the value. The default value is 260 if the key is not there yet.

Before the change was effective, I needed to reboot the server though.



来源:https://stackoverflow.com/questions/42243211/increase-max-url-length-in-asp-net-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!