Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Increase max url length in asp.net core

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.

like image 833
mjepson Avatar asked Feb 15 '17 07:02

mjepson


People also ask

Is there a max size for a URL?

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.

What is the default maximum length for URL characters in asp net?

Property Value The length of the URL, in number of characters. The default is 260.

What is the maximum size of query string in asp net?

The maximum length of the query string, in number of characters. The default is 2048.


2 Answers

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.

like image 178
mjepson Avatar answered Sep 21 '22 22:09

mjepson


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

like image 39
armyllc Avatar answered Sep 20 '22 22:09

armyllc