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.
The official documentation specifies a maximum length of 2048 characters for the <loc> element, which is used to submit URLs: URL of the page. This URL must begin with the protocol (e.g. “http”) and end with a trailing slash if required by the web server. This value must not exceed 2,048 characters.
Property Value The length of the URL, in number of characters. The default is 260.
The maximum length of the query string, in number of characters. The default is 2048.
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.
The accepted solution did not work for me. I am running asp.net core 2.0 on iis express locally. What worked is configuring Kestrel "directly" in startup.cs. I did not need to change the registry or reboot the machine.
var host = new WebHostBuilder()
.UseKestrel(options => {
options.Limits.MaxRequestBufferSize = 302768;
options.Limits.MaxRequestLineSize = 302768;
})
I added to my web.config the following (although i am not sure it made the difference.)
<requestLimits maxAllowedContentLength="1073741824" maxUrl="20480" maxQueryString="20480" />
This is where i found it: https://titanwolf.org/Network/Articles/Article?AID=2fa1aad6-6b3d-479e-9770-a0850497d270
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With