属性路由为api / delete / 1提供404

时间:2014-03-23 14:22:02

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

我正在使用web api作为后端,AngularJS作为SPA。

我在WebApi2中使用属性路由。问题是,当我执行任何与以下骨架匹配的http请求时,它会抛出404 NOT Found。

请求:http://localhost:63915/api/cab/delete/2

请求: Request

enter image description here

错误:enter image description here

WebApiConfig代码:

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

    }
}

RouteDeclaration:

[RoutePrefix("api/driver")]
public class DriverController : ApiController
{
    [POST("new")]
    public bool NewDriver(DriverModel driver)
    {
        return new DatabaseService().CreateNewDriver(driver);
    }

    [GET("all")]
    public List<DriverModel> GetAllDrivers()
    {
        return new DatabaseService().GetDriverList();
    }

    [DELETE("delete/{id}")]
    public bool Delete(int id)
    {
        return new DatabaseService().DeleteDriver(id);
    }
}

如果我执行类似api/driver/delete?id=2的操作并将路线更改为[DELETE("delete")]则可行。

我的配置一切都好吗?

我认为问题可能只出在我的配置上。请告诉我我错过了什么使路线工作。

1 个答案:

答案 0 :(得分:0)

我添加了另一条到WebApiConfig的路线,它可以工作!

config.Routes.MapHttpRoute(
            name: "ComplexApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );