1

重複キーに関する以下のエラーが発生し続けるのはなぜですか? これは、属性ルーティングを試みる前は正常に機能していましたが、現在は機能していません。削除する{id:int}と、ID が指定されていなくても、常に 2 番目のメソッドにヒットし、最初のメソッドにはヒットしません。

イベントコントローラー

[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

答えではありませんが、元に戻りました。元の問題は、2 つの API があることだと思います。名前空間の Web API ルートを使用できないため、EventsController2 回リストされているが異なる名前空間の下にあるため、キーの問題が重複していました。それは重複キーの問題{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 に答える