【发布时间】:2013-11-12 20:10:21
【问题描述】:
为什么我不断收到以下关于重复键的错误?在我尝试属性路由之前,这很好用,但现在不行。如果我删除{id:int},它总是会使用第二种方法,而不是第一种,即使没有提供 ID。
事件控制器
[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;
}
访问 /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());
}
}
}
【问题讨论】:
-
基于您在上面提供的控制器,我尝试复制它,但我无法...您能否分享此控制器中其余操作的骨架结构?从错误消息中,似乎正在将相同的键添加到字典中...您可以分享完整的堆栈跟踪吗?
-
添加了完整的堆栈跟踪。
-
谢谢 Mike ..你能分享一下你的 WebApiConfig.cs 的样子吗......从错误看起来你正试图在字典中添加一个名为
int的新内联约束......是吗? -
添加了 webapiconfig.cs
-
嗯,很奇怪..我只是无法根据上面的信息重现您的问题...堆栈跟踪清楚地表明当重复键被添加到字典时发生了异常。 ..如果您注释掉具有
{id:int}的路线,您是否看到您的应用程序工作?...我唯一的猜测是MapHttpAttributeRoutes可能会被多次调用...我从堆栈跟踪中看到Ninject,想知道它是否在做通过多次调用配置来获得一些时髦的东西...顺便说一下,内联约束仅适用于非查询字符串路由数据...
标签: asp.net-mvc asp.net-web-api asp.net-mvc-5 attributerouting