Why do I keep getting the error below about duplicate keys? This worked fine before I tried attribute routing, but now it does not. If I remove {id:int}
it always hits the second method, never the first, even if no ID is supplied.
EventsController
[System.Web.Http.RoutePrefix("api/v1/events")]
public partial class EventsController : System.Web.Http.ApiController
{
[System.Web.Http.Route("")]
public virtual ApiEventsResponse Get([FromUri] ApiEventsRequest request)
{
.....
return response;
}
[System.Web.Http.Route("{id:int}")]
public virtual ApiEventResponse Get(int id, [FromUri] ApiEventRequest request)
{
.....
return response;
}
Error when accessing /api/v1/events?id=12315
Server Error in '/' Application.
An item with the same key has already been added.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.ArgumentException: An item with the same key has already been added.
Source Error:
Line 407: AreaRegistration.RegisterAllAreas();
Line 408:
Line 409: GlobalConfiguration.Configure(WebApiConfig.Register);
Line 410: RegisterRoutes(RouteTable.Routes);
Line 411:
Source File: f:\My Webs\BasketballTournaments\MainBranch\Websites\Tournaments\Global.asax.cs Line: 409
Stack Trace:
[ArgumentException: An item with the same key has already been added.]
System.ThrowHelper.ThrowArgumentException(ExceptionResource resource) +52
System.Collections.Generic.Dictionary`2.Insert(TKey key, TValue value, Boolean add) +11187358
System.Collections.Generic.Dictionary`2.Add(TKey key, TValue value) +10
System.Web.Http.Routing.InlineRouteTemplateParser.ParseRouteTemplate(String routeTemplate, IDictionary`2 defaults, IDictionary`2 constraints, IInlineConstraintResolver constraintResolver) +363
System.Web.Http.Routing.HttpRouteBuilder.BuildParsingRoute(String routeTemplate, Int32 order, IEnumerable`1 actions) +86
System.Web.Http.HttpConfigurationExtensions.MapHttpAttributeRoutesInternal(HttpConfiguration configuration, HttpRouteBuilder routeBuilder) +232
System.Web.Http.<>c__DisplayClass5.<MapHttpAttributeRoutes>b__4() +13
System.Web.Http.Routing.RouteCollectionRoute.EnsureInitialized(Func`1 initializer) +70
System.Web.Http.<>c__DisplayClass5.<MapHttpAttributeRoutes>b__3(HttpConfiguration config) +63
System.Web.Http.HttpConfiguration.EnsureInitialized() +23
System.Web.Http.GlobalConfiguration.Configure(Action`1 configurationCallback) +57
Tournaments.MvcApplication.OnApplicationStarted() in f:\My Webs\BasketballTournaments\MainBranch\Websites\Tournaments\Global.asax.cs:409
Ninject.Web.Common.NinjectHttpApplication.Application_Start() in c:\Projects\Ninject\Ninject.Web.Common\src\Ninject.Web.Common\NinjectHttpApplication.cs:82
[HttpException (0x80004005): An item with the same key has already been added.]
System.Web.HttpApplicationFactory.EnsureAppStartCalledForIntegratedMode(HttpContext context, HttpApplication app) +9903113
System.Web.HttpApplication.RegisterEventSubscriptionsWithIIS(IntPtr appContext, HttpContext context, MethodInfo[] handlers) +118
System.Web.HttpApplication.InitSpecial(HttpApplicationState state, MethodInfo[] handlers, IntPtr appContext, HttpContext context) +172
System.Web.HttpApplicationFactory.GetSpecialApplicationInstance(IntPtr appContext, HttpContext context) +336
System.Web.Hosting.PipelineRuntime.InitializeApplication(IntPtr appContext) +296
[HttpException (0x80004005): An item with the same key has already been added.]
System.Web.HttpRuntime.FirstRequestInit(HttpContext context) +9882460
System.Web.HttpRuntime.EnsureFirstRequestInit(HttpContext context) +101
System.Web.HttpRuntime.ProcessRequestNotificationPrivate(IIS7WorkerRequest wr, HttpContext context) +254
WebApiConfig.cs
namespace Tournaments.App_Start
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Filters.Add(new ExceptionHandlingAttribute());
}
}
}
Not an answer but I just went back to my original. I think the original problem is I have two apis, and since you cant namespace web api routes yet there were duplicate key issues because the EventsController
was listed twice but under different namespaces. That was the duplicate key issue, but for the {id:int}
never working for a query string you got me.
routes.MapHttpRoute(
"DefaultApi",
"api/v{version}/{controller}/{id}",
new { id = RouteParameter.Optional, version = 1 }
);
routes.MapHttpRoute(
"DefaultApiAction",
"api/v{version}/{controller}/{action}"
);