无法找到mvc中的api控制器

时间:2016-04-26 06:40:42

标签: asp.net-mvc asp.net-web-api

我正在尝试开发一个webapi+angularjs+mvc项目。您可以看到我的apicontroller

 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

1 个答案:

答案 0 :(得分:2)

添加行

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

这将注册webapi路线