我想访问以下方法,但我无法访问" 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
&#34; http://www.test.com:46707/rpc/RealmStatus/RealmByPopulationName/2/Vindication&#34;的结果:
<Error>
未找到与请求URI匹配的HTTP资源&#39; http://www.test.com:46707/rpc/RealmStatus/RealmByPopulationName/2/Vindication&#39;。 在控制器上没有找到任何操作&#39; RealmStatus&#39;与请求匹配。
答案 0 :(得分: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+$)" }
);