简体   繁体   中英

How do I get WebAPI to accept a complex object as its argument for a DELETE HTTP verb without using C# attributes?

I'm using C#/WebAPI on the server and AngularJS on the client.

Given a method like public void Delete(MyClass m) on a WebAPI controller and a class MyClass that is composed of value objects how do I format an HTTP DELETE request using $http without specifying an attribute in C# (ie [FromUri] ?

Here's why I'm using a hierarchical object and not an ID:

I am developing a plugin system that I am trying to make as simple as possible for the consuming programmer, in terms of time spent learning, time spent writing and code length.

The plugin system will allow CRUD access to EF objects, essentially. On the server I'll have them implement an interface when I'd like to just be:

public interface IMyInterface<TEntity>
{
    IEnumerable<TEntity> Get();
    void Put(TEntity t);
    void Post(TEntity t);
    void Delete(TEntity t);
}

I'm working on Delete right now on the client side and for some reason a complex object isn't being mapped over without a C# attribute in the concrete implementation like [FromUri] . As I understand attributes are not inherited from interfaces and thus, if my client (AngularJS) does something like $http.delete('api/uri', { params: { objectToDelete } }); an attribute will be necessary to get that delete working, using params. I've also tried using the data property of the config object and that didn't work, either. I know it's traditional/standard to have an ID as the argument to a Delete method but if such a thing exists here then the client will need to be aware what constitutes a "key" - which is more for the consuming programmer to write.

MyClass is a complex type and is bound from the request body with ASP.NET Web API. HTTP semantics do not allow a request body for DELETE and for this reason, you will not be able to post a request body with DELETE using Angular. To be exact, this is not an issue with Angular but most of the browsers do not send a body with AJAX DELETE requests.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM