2017-03-14 116 views
0

我已经定义了以下路线:ASP.Net的WebAPI路由配置

GlobalConfiguration.Configuration.Routes.Add(
    "iOS Service", 
    new HttpRoute("ios/{controller}/{action}/{id}", new HttpRouteValueDictionary { { "id", RouteParameter.Optional } }) 
); 

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service Documents", 
    routeTemplate: "ios/getfulldocumentstructure", 
    defaults: new { controller = "Documents", action = "GetFullDocumentStructure" } 
); 

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service AppInfo", 
    routeTemplate: "ios/appinfo", 
    defaults: new { controller = "AppInfo", action = "GetAppInfo" } 
); 

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service Html", 
    routeTemplate: "ios/html/{language}/{contentId}", 
    defaults: new { controller = "Html", action = "GetHtml", language = RouteParameter.Optional, contentId = RouteParameter.Optional } 
); 

用下面的控制器:

public class HtmlController : ApiController 
{ 
    [HttpGet] 
    public string GetHtml(long language, long contentId) 
    { 

     return "Hello"; 
    } 
} 

如果我打了使用http://localhost/ios/html?languageId=1033&contentId=12345的GetHtml动作火灾的服务。

如果我使用http://localhost/ios/html/1033/12345命中该服务,我收到一个错误,指出在控制器中找不到匹配的操作。

我做错了什么?

+2

您是否定义了可能导致冲突的其他路径?这条路线相对于其他路线定义在哪里? – Nkosi

+0

我有三个其他路由定义,但错误消息说它击中正确的控制器 “消息”:“没有找到与请求URI'http:// localhost/ios/html/1033/2匹配的HTTP资源' 。“, ”MessageDetail“:”控制器'Html'上找不到与名称'1033'匹配的操作。“ } –

+1

仅仅因为它击中了正确的控制器并不意味着它正在击中正确的路线。请张贴你的其他路线和他们的登记顺序。 – NightOwl888

回答

0

移动捕获到最后所有路线固定的问题。电话打错了控制器:

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service Documents", 
    routeTemplate: "ios/getfulldocumentstructure", 
    defaults: new { controller = "Documents", action = "GetFullDocumentStructure" } 
); 

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service AppInfo", 
    routeTemplate: "ios/appinfo", 
    defaults: new { controller = "AppInfo", action = "GetAppInfo" } 
); 

GlobalConfiguration.Configuration.Routes.MapHttpRoute(
    name: "iOS Service Html", 
    routeTemplate: "ios/html/{language}/{contentId}", 
    defaults: new { controller = "Html", action = "GetHtml", language = RouteParameter.Optional, contentId = RouteParameter.Optional } 
); 

GlobalConfiguration.Configuration.Routes.Add(
    "iOS Service", 
    new HttpRoute("ios/{controller}/{action}/{id}", new HttpRouteValueDictionary { { "id", RouteParameter.Optional } }) 
); 
0

如果你的参数是可选的,那么也许试试这个:

public class HtmlController : ApiController 
{ 
    [HttpGet] 
    public string GetHtml(long? languageId, long? contentId) 
    { 

     return "Hello"; 
    } 
}