0

Is it possible to set a maximum query(string) length for a single route/action in ASP.NET Core?

I have a search action that returns entities keyed with a Guid in the database. I have an requirement to return a number of them from the API, retrieved by Id. So I would like to modify the search action to allow a parameter [FromQuery]Guid[] EntityIds, and then I could pass 10 or 20 (or whatever) Entity Ids to this search action. However, the API is failing because the querystring is too long. I know it can be set globally, but I was hoping there would be something like:

[HttpGet, MaxQueryLength(4000)]
public async Task<IActionResult> Search([FromQuery] Guid[] EntityIds)

So I don't have to change the whole API for this one request...?

Sean
  • 14,359
  • 13
  • 74
  • 124
  • 2
    I don't think it is possible, it is more of a web server side setting. I would consider moving to Post request and send EntityIds in Body to avoid troubles with Url length – Nikolay Mar 15 '23 at 12:46
  • Maybe you can refer to this [link](https://stackoverflow.com/questions/42243211/increase-max-url-length-in-asp-net-core) – Xinran Shen Mar 16 '23 at 02:00

1 Answers1

0

Just guessing, but how about ?

        public async Task<IActionResult> Search([FromQuery, MaxLength(4000)] Guid[] EntityIds)

The MaxLength Attribute should do what you like ... From the .Net 6 ressources:

    /// <summary>
    ///     Initializes a new instance of the <see cref="MaxLengthAttribute" /> class.
    /// </summary>
    /// <param name="length">
    ///     The maximum allowable length of collection/string data.
    ///     Value must be greater than zero.
    /// </param>
    [RequiresUnreferencedCode(CountPropertyHelper.RequiresUnreferencedCodeMessage)]
    public MaxLengthAttribute(int length)
        : base(() => DefaultErrorMessageString)
    {
        Length = length;
    }
Martin Hertig
  • 145
  • 1
  • 10
  • No, unfortunately not. It gives `414 - URI too long` before it even gets to the controller action. Needs to be earlier in the pipeline, I guess. – Sean Mar 15 '23 at 12:36
  • Hm as stated in another answer, then it really seems to be globally set server-side. You could change the web.config as in Xinran Shen's link... Another possibiliy would be to pass with the parameters in a list in the body instead of the query string ... ? – Martin Hertig Mar 16 '23 at 05:30