2016-04-26 107 views
0

我試圖建立一個webapi+angularjs+mvc項目。在這裏你可以看到我apicontrollerMVC中的API控制器無法找到

public class DefaultController : ApiController 
    { 
     testDBEntities a = new testDBEntities(); 

     public IEnumerable<City> Get() 
     { 
      return a.Cities; 
     } 
    } 

在這裏你可以看到webapiconfig

public static class WebApiConfig 
    { 
     public static void Register(HttpConfiguration config) 
     { 
      config.MapHttpAttributeRoutes(); 

      config.Routes.MapHttpRoute(
       name: "DefaultApi", 
       routeTemplate: "api/{controller}/{id}", 
       defaults: new { id = RouteParameter.Optional } 
      ); 

     } 
    } 

但是,當我輸入:localhost:5411/api/default

我得到了這個錯誤:

The resource cannot be found. 

Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly. 

Requested URL: /api/default 
+0

您正在使用什麼版本的WebAPI的? –

+0

@AlexArt。我怎樣才能找到版本? –

+0

始終在路由路徑中使用{action},如 - routeTemplate:「api/{controller}/{action}/{id}」。這是因爲,如果你沒有指定{action}你的控制器中不能有多個get方法 –

回答

2

添加行

GlobalConfiguration.Configure(WebApiConfig.Register) in your Global.asax.cs file 

這將註冊的WebAPI路線

+0

WebApi具有'Startup.cs'而不是'global.asax.cs' –

+0

@Alex,當創建一個新項目時,當選擇mvc和webapi時,只添加global.asax,而不是Startup.cs –

+0

@AlexArt。我無法在startup.cs中找到GlobalConfiguration.Configure(WebApiConfig.Register) –

相關問題