0

我想访问以下方法,但我无法访问“ http://www.test.com:46707/rpc/RealmStatus/RealmByPopulationName/2/Vindication

    [ActionName("RealmByPopulationName")]
      public IEnumerable<MyRealmStatus> GetRealmsByBattleGroupName(int regionid, string battlegroupname)
      {
             //dosomething...
       }

我的 webApiConfig.cs 下面

 public static class WebApiConfig
{
    public static void Register(HttpConfiguration config)
    {
        config.Routes.MapHttpRoute(
            name: "DefaultApi",
            routeTemplate: "api/{controller}/{regionid}",
            defaults: new { id = RouteParameter.Optional }
        );

        config.Routes.MapHttpRoute(
            name: "GetData",
            routeTemplate: "api/{controller}/{regionid}/{id}"
        );


         config.Routes.MapHttpRoute(
             name: "RpcApi",
             routeTemplate: "rpc/{controller}/{action}/{regionid}",
             defaults: new { action = "Get" }
        );

         config.Routes.MapHttpRoute(
             name: "RpcApi2",
             routeTemplate: "rpc/{controller}/{action}/{regionid}/{quality}/",
             defaults: new { action = "Get" }
        );

         config.Routes.MapHttpRoute(
            name: "RpcApi3",
            routeTemplate: "rpc/{controller}/{action}/{regionid}/{battlegroupname}/",
            defaults: new { action = "Get" }
       );
    }
}

发生错误我无法访问 GetRealmsByBattleGroupName

http://www.test.com:46707/rpc/RealmStatus/RealmByPopulationName/2/Vindication ”的结果:

<Error>

未找到与请求 URI ' http://www.test.com:46707/rpc/RealmStatus/RealmByPopulationName/2/Vindication '匹配的 HTTP 资源。在与请求匹配的控制器“RealmStatus”上未找到任何操作。

4

1 回答 1

1

您的呼叫将首先匹配此路由:

config.Routes.MapHttpRoute(
    name: "RpcApi2",
    routeTemplate: "rpc/{controller}/{action}/{regionid}/{quality}/",
    defaults: new { action = "Get" }
);

因此,您的 API 正在 RealmStatus 控制器上寻找与以下签名匹配的方法:

[ActionName("RealmByPopulationName")]
GetRealmsByBattleGroupName(int regionid, string quality)

如果质量是一个数字,您可以通过添加以下约束来区分路线:

config.Routes.MapHttpRoute(
    name: "RpcApi2",
    routeTemplate: "rpc/{controller}/{action}/{regionid}/{quality}/",
    defaults: new { action = "Get" },
    constraints: new { id = @"(^\d+$)" }
);
于 2013-09-12T15:25:32.317 回答