Asp.net Web API属性路由404错误

时间:2014-05-13 13:17:58

标签: c# asp.net asp.net-web-api asp.net-web-api-routing

我无法弄清楚为什么我的属性路由不起作用。

这是我的设置:

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

        // Convention-based routing
        config.Routes.MapHttpRoute(
            name: "API Default",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );

    }
}

这是我的控制器,带有我的路由属性:

[Route("api/v1.0/orders")]
public class OrdersV1Controller
{

    [APIAuthentication(RequireAuthentication = true)]
    [HttpGet]
    [Route("{id:int}")]
    public GetOrderResponse GetOrder(int id)
    { 
      .....
    }
}

这是我的全局asax文件:

public class Global : HttpApplication
{
    void Application_Start(object sender, EventArgs e)
    {
        AreaRegistration.RegisterAllAreas();
        GlobalConfiguration.Configure(WebApiConfig.Register);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.DefaultValueHandling = Newtonsoft.Json.DefaultValueHandling.Populate;
    }
}

以下是我正在测试的网址,该网址返回了404未找到的内容:

http://localhost:60105/api/v1.0/orders/111111

1 个答案:

答案 0 :(得分:7)

您的控制器需要是API控制器:

public class OrdersV1Controller : ApiController