Aspnetcore: RouteTemplate containing 29 or more parts causes OverflowException

Created on 26 Mar 2018  路  8Comments  路  Source: dotnet/aspnetcore

When a route has 29 or more parts to it, an OverflowException exception occurs (OverflowException: Value was either too large or too small for a Decimal).

This issue comes from a Stack Overflow question.

To reproduce the issue, create a function which contains 29 or more parts (e.g. route to controller plus multiple parameters, as in the example below; full code available here):

[Route("api/[controller]")]
public class ValuesController : Controller
{
    [HttpGet("{a}/{b}/{c}/{d}/{e}/{f}/{g}/{h}/{i}/{j}/{k}/{l}/{m}/{n}/{o}/{p}/{q}/{r}/{s}/{t}/{u}/{v}/{w}/{x}/{y}/{z}/{a2}/{b2}")]
    public string Get(int a, int b, int c, int d, int e, int f, int g, int h, int i, int j, int k, int l, int m, int n, int o, int p, int q, int r, int s, int t, int u, int v, int w, int x, int y, int z, int a2, int b2) =>
        $"Hello {a} {b} ...";
}

Per Evk's answer on the same post, the issue is caused by RoutePrecedence.cs's ComputeOutbound method, which contains the code: (decimal)Math.Pow(10, i), causing an overflow when i is 29 or greater.

area-mvc bug

All 8 comments

Oh boy. 29 segments?

We should probably just disallow having more than "X" number of route parameters. This type of pattern seems more suited to using something like a query string.

@Eilon agreed it may make more sense to amend the spec than try to provide support for a much larger set of segments. However, it would be good to add a more meaningful exception; e.g.

if (template.Segments.Count > 28)
    throw (new ArgumentException ("A route template can have no more than 28 segments") { Data = {
        {"NoOfSegments" , template.Segments.Count}
        ,{"Segments" , template.Segments}
    }});

Yup agreed, a better error experience would make sense. Probably not a super high priority - in over 10 years of Routing existing, until now I've never even seen anyone try. (Granted, it didn't used to be an error, but I still never saw it.)

@JamesNK, @rynowak is throwing a better exception is something you'd like to consider for the Endpoint routing (unless it's already the case)?

This continues to overflow with endpoint routing. It is simple enough to throw a better exception.

Fixed in 3.0! Thanks for detailed bug description @JohnLBevan

Thank you for the fix @JamesNK

Was this page helpful?
0 / 5 - 0 ratings