mccalltd / AttributeRouting

Define your routes using attributes on actions in ASP.NET MVC and Web API.
http://mccalltd.github.io/AttributeRouting/
MIT License
416 stars 89 forks source link

Poor handling of malformed route - Index was outside the bounds of the array. #287

Open dotnetchris opened 10 years ago

dotnetchris commented 10 years ago

If you have a malformed AttributeRouting Route you get a very ungraceful error.

Suppose i have the route:

[GET("contents/{page-slug}/{blog-title")]

Note the lack of a closing } on blog-title.

This results in the exception

[IndexOutOfRangeException: Index was outside the bounds of the array.]
   AttributeRouting.Framework.RouteBuilder.RemoveQueryString(String url) +149
   AttributeRouting.Framework.RouteBuilder.CreateRouteDefaults(RouteSpecification routeSpec, IDictionary`2& defaults, IDictionary`2& queryStringDefaults) +296
   AttributeRouting.Framework.<BuildRoutes>d__4.MoveNext() +143
   System.Linq.<SelectManyIterator>d__14`2.MoveNext() +507
   System.Linq.<CastIterator>d__b1`1.MoveNext() +296
   System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) +536
   System.Linq.Enumerable.ToList(IEnumerable`1 source) +80
   AttributeRouting.Web.Mvc.RouteCollectionExtensions.MapAttributeRoutesInternal(RouteCollection routes, Configuration configuration) +104
   Web.Site.AttributeRoutingConfig.RegisterRoutes(RouteCollection routes) in c:\Projects\myproj\src\Site\App_Start\AttributeRoutingConfig.cs:15
   Web.Site.AttributeRoutingConfig.Start() in c:\Projects\myproj\src\Site\App_Start\AttributeRoutingConfig.cs:20

This exception makes no sense to a user of AR. I have a route with no query string, why am i blowing up in RemoveQueryString?

I understand it might be very difficult to build a parser that verifies the string is valid. At the very least i think you need to do boundary checking in RemoveQueryString and throw a MalformedRouteException() or PossiblyMalformedRouteException() etc.