1

为什么我不断收到以下有关重复键的错误?在我尝试属性路由之前,这很好用,但现在不行。如果我删除{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());
        }
    }
}
4

1 回答 1

0

不是答案,但我只是回到了原来的状态。我认为最初的问题是我有两个 api,并且由于您无法命名 web api 路由但存在重复的关键问题,因为它们EventsController被列出了两次但在不同的命名空间下。那是重复键的问题,但是对于{id:int}从不为查询字符串工作的问题,您得到了我。

routes.MapHttpRoute(
            "DefaultApi",
            "api/v{version}/{controller}/{id}", 
            new { id = RouteParameter.Optional, version = 1 }
        );

        routes.MapHttpRoute(
            "DefaultApiAction",
            "api/v{version}/{controller}/{action}"
        );
于 2013-11-12T23:20:13.807 回答