我试图建立一个webapi+angularjs+mvc
项目。在这里你可以看到我apicontroller
MVC中的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
您正在使用什么版本的WebAPI的? –
@AlexArt。我怎样才能找到版本? –
始终在路由路径中使用{action},如 - routeTemplate:“api/{controller}/{action}/{id}”。这是因为,如果你没有指定{action}你的控制器中不能有多个get方法 –