2014-04-11 42 views
0

我有一個Web API 2,我正在使用jQuery Ajax並查看文檔,現在可以在Controller內部包含一個路由,以便您可以在GET請求中包含其他參數例如。Web API 2 404使用jQuery Ajax時出錯

我曾嘗試加入這個例如:

[Route("api/formHTML/{id}/{code}/{value}")] 
     public IEnumerable<string> Get(int id, int code, int value) 
     { 
      return new string[] {"<html><head></head><body>Test</body></html>"}; 
     } 

這將基本上返回一些HTML代碼。

我不斷收到404 API使用GET時未找到/ formHTML/1/5/2

我仍然有這個在我的Global.asax包含默認路由映射。

protected void Application_Start(object sender, EventArgs e) 
    { 
     GlobalConfiguration.Configuration.Routes.MapHttpRoute(
     name: "DefaultApi", 
     routeTemplate: "api/{controller}" 
     ); 
    } 

有誰知道我爲什麼會得到404?

我知道我可以通過將它們作爲數據對象發送到獲取請求中並在Web API中使用formBody來獲取值,但我最好使用上述約定。

感謝

回答

1

我必須再次閱讀文檔解決了這個問題,我感動的默認映射關係WebApiConfig.cs,然後調用此方法從在Global.asax:

GlobalConfiguration.Configure(WebApiConfig.Register); 

WebApiConfig.cs包含:

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

我的問題已解決!