2013-03-16 2 views
4

새 WebAPI MVC 프로젝트를 만들었습니다. API 컨트롤러에 경로가 http://localhost:1234/api이고이 경로에서 작동하지만 RegisterRoutes 클래스는 작동하지 않습니다. 기본 경로가 포함되어 있으면 다음을 포함합니다.새 WebApi 프로젝트에 API에 대한 기본 라우팅이 없습니다. (여전히 작동 함)

public static void RegisterRoutes(RouteCollection routes) 
{ 
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 
    routes.MapRoute(
     name: "Default", 
     url: "{controller}/{action}/{id}", 
     defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional } 
    ); 
} 

API 라우팅은 어디에 있습니까?

routes.MapHttpRoute(
    name: "API Default", 
    routeTemplate: "api/{controller}/{id}", 
    defaults: new { id = RouteParameter.Optional } 
); 

당신은 App_Start 디렉토리에 배치됩니다 WebApiConfig.cs 파일이 찾을 수

건배

데이브

답변

1

그것은 다른 클래스에 살고 : 나는 그것이 있었다 나 내가 같은 올바른 빨리 그것을 표시 한 것이다 클래스가 실제로 쓴 거라고 보지 못했다

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Web.Http; 

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

      // Uncomment the following line of code to enable query support for actions with an IQueryable or IQueryable<T> return type. 
      // To avoid processing unexpected or malicious queries, use the validation settings on QueryableAttribute to validate incoming queries. 
      // For more information, visit http://go.microsoft.com/fwlink/?LinkId=279712. 
      //config.EnableQuerySupport(); 

      // To disable tracing in your application, please comment out or remove the following line of code 
      // For more information, refer to: http://www.asp.net/web-api 
      config.EnableSystemDiagnosticsTracing(); 
     } 
    } 
}