代码之家  ›  专栏  ›  技术社区  ›  Raju Mandapati

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

  •  0
  • Raju Mandapati  · 技术社区  · 10 年前

    我正在做一个SPA,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 }
            );
            
        }
    }
    

    路线声明:

    [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 回复  |  直到 4 年前
        1
  •  0
  •   Raju Mandapati    10 年前

    我向WebApiConfig添加了另一条路由,它可以工作!

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