How to map different HTTP methods on the same URL for different controllers?

I have my own API for a small part of the application, divided into two controllers, due to the (external) requirements for the JSON data wrapper in the API (some requests must use camelCasing, while others must use PascalCasing).

Now I have a URL that I want to map to PascalCasing for GET, but camelCasing for PUT, so I tried the following:

[PascalCasing] // custom attribute, part of our code
               // We configure all controllers that *don't* have this to use
               // camelCasing
public class PascalCasedController : ApiController
{
    [HttpGet]
    [Route("url/to/my/resource/{id}")]
    public IHttpActionResult(int id)
    {
        return Ok(GetResource(id));
    }
}

public class CamelCasedController : ApiController
{
    [HttpPut]
    [Route("url/to/my/resource/{id}")]
    public IHttpActionResult(int id, Resource resource)
    {
        SaveResource(id, resource);
        return Ok();
    }
}

The request GETworks as expected, but if I try PUTsomething there with Fiddler, I get the following error message:

, URL. , URL.
:
 MyProject.PascalCaseController
 MyProject.CamelCaseController

, , , , WebAPI , , HTTP , . , WebAPI, , ?

+4
1

@Tomas - System.Web.Http "System.Web.Http.Dispatcher.IHttpControllerSelector". HttpControllerSelector. DefaultControllerSelector HttpConfiguration AreaRegistration.

httpConfig.Services.Replace(typeof (IHttpControllerSelector), CustomControllerSelector (services.GetHttpControllerSelector()));

SelectController() IHttpControllerSelector, GetControllerMapping() IHttpControllerSelector. . DeclareMethods CustomAttributes DeclareMethods. HttpGetAttribute, HttpPutAttribute.

HttpRequestMessage (GET/PUT) CustomAttributes. URL- Http-, HttpControllerDiscriptor SelectController().

URL- .

0

All Articles