Search code examples
asp.net-web-apiasp.net-web-api-routing

HTTP Method not allowed (405) occurs on DELETE when slash in id


This is the route configuration:

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

The method signature of the controller method:

    // DELETE
    public void Delete(string clientId, string id)

Pretty much standard so far except for the id being of type string.

Everything works fine until an id contains a slash: MI-01/02

So the HTTP request looks like this:

DELETE http://de2sv034.xnet.oe.olympus/corporate/ExtraNet/API/dev/M01/Products/MI-01/02

Web-API emits HTTP-status code 405 (method not allowed). System.Uri.EscapeDataString on client side doesn't help because slash isn't escaped.

How can I get the route mapped properly even if id contains a slash?


Solution

  • It'll work if you specify your id directly inside the URI as a query string parameter (it doesn't even need to be URL encoded). For example, something like this in your case: .../Products?id=MI-01/02

    It works because anything after the question mark (?) is no longer interpreted as part of the main path but as a query string parameter (those are separated by an ampersand & if you have more than one). For more information about parameter binding, see this article.