2017-03-14 157 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"; 
    } 
}