我在我的asp.net mvc5 web项目中添加了一个Api Controller,以及其他控制器......我正在不断地找到资源找不到错误。请指出我可能犯的错误......
以下是我的WebApi路线的注册方法
public static void Register(HttpConfiguration config)
{
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
这是Global.asax.cs
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
//GlobalConfiguration.Configure(WebApiConfig.Register);
WebApiConfig.Register(GlobalConfiguration.Configuration);
}
这是我的Api控制器类
public class SearchController : ApiController
{
public IEnumerable<string> GetSearch()
{
return new string[] { "value1", "value2" };
}
}
&#34; http://sukhdev.com/api/search&#34;是我用来调用web api的url方案,但它坚持不懈&#34;无法找到资源。&#34;
答案 0 :(得分:10)
你为什么评论//GlobalConfiguration.Configure(WebApiConfig.Register);
protected void Application_Start()
{
AreaRegistration.RegisterAllAreas();
FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
BundleConfig.RegisterBundles(BundleTable.Bundles);
}