Question

I am doing a SPA with web api as the backend and AngularJS as the SPA.

I am using attribute routing in WebApi2. The problem is when I do any http request that matches the following skeleton, it throws me a 404 NOT Found.

Request: http://localhost:63915/api/cab/delete/2

Request: Request

enter image description here

Error:enter image description here

WebApiConfig Code:

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);
    }
}

If I do something like api/driver/delete?id=2 and change the route to [DELETE("delete")] it works.

Is everything all right with my config ?

I think the problem might be with my config only. Please tell me what I am missing to make the route work.

Était-ce utile?

La solution

I added another route to the WebApiConfig and it works!

config.Routes.MapHttpRoute(
            name: "ComplexApi",
            routeTemplate: "api/{controller}/{action}/{id}",
            defaults: new { id = RouteParameter.Optional }
        );
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top