Multiple PUT Methods in ASP.NET Web API

I have a controller Groupswith the following actions:

public GroupModel Get(int ID)

public GroupModel Post(CreateGroupModel model)

public void Put(PublicUpdateGroupModel model)

public void PutAddContacts(UpdateContactsModel model)

public void PutRemoveContacts(UpdateContactsModel model)

public void Delete(int ID)

And what I would like to do is use standard REST routing to call the standard get, post, put, delete mehods commands. But name PutAddContactsand PutRemoveContactsif action names are appended to the URL, for example:

GET groups / - calls Get method

POST groups / - calls Submission method

PUT groups / - calls put method

DELETE groups / - calls Delete method

PUT groups / addcontacts - Calls the PutAddContacts method

PUT groups / removeecontacts - Calls the PutRemoveContacts method

Is it possible to configure routing for this, or do I need to go down the RPC route for routing if I want to use action names in my URLs?

+5
2


, , RPC. , RPC. WebAPI RESTful, , . , - MVC:

routes.MapRoute( name    : "Default",       
                 url     : "{controller}/{action}/{id}",
                 defaults: new { controller = "Home", 
                                 action     = "Index", 
                                 id         = UrlParameter.Optional });

MVC, . , RESTful, , ...

RESTful
REST HTTP, . REST , . HTTP URI, HTTP. , , HTTP GET , HTTP GET HTTP, ​​ .

POST/PUT vs MERGE/PATCH
GET, POST, PUT, HEAD .. HTTP. , GET , POST - , PUT - ( ). , : . , , - ? , .

RESTful, , . , MERGE "", "". , , PUT, .


, . . ( ), . - ...

//api/Group/
public List<GroupModel> Get()
public GroupModel Get(int ID)
public GroupModel Post(GroupModel model)  //add a group
public GroupModel Put(GroupModel model)   //update a group (see comments above)
public void Delete(int ID)


//api/GroupContacts/
public ContactsModel Get()                    //gets complete list
public void PostContacts(ContactsModel model) //pushes a COMPLETE new state
public void Delete()                          //delete entire group of contacts


//api/GroupContact/354/
public ContactModel Get(int id)             //get contact id #354
public void PostContact(ContactModel model) //add contact (overwrite if exits)
public void Delete(int id)                  //delete contact if exists

, URL- (: /api/Group/Contacts, /api/Group/Contact), , . IMHO, asp.net , ... ; -)

+11

, EBarr, Web API . , -API. Nuget , GitHub

, URI . , URI , .

, , URI, , :

internal class Program
{
   private static void Main(string[] args)
   {
    var baseAddress = new Uri("http://oak:8700/");

    var configuration = new HttpSelfHostConfiguration(baseAddress);
    var router = new ApiRouter("api", baseAddress);

    // /api/Contacts
    router.Add("Contacts", rcs => rcs.To<ContactsController>());

    // /api/Contact/{contactid}
    router.Add("Contact", rc =>
                          rc.Add("{contactid}", rci => rci.To<ContactController>()));

    // /api/Group/{groupid}
    // /api/Group/{groupid}/Contacts
    router.Add("Group", rg =>
                        rg.Add("{groupid}", rgi => rgi.To<GroupController>() 
                                                       .Add("Contacts", rgc => rgc.To<GroupContactsController>())));


    configuration.MessageHandlers.Add(router);

    var host = new HttpSelfHostServer(configuration);
    host.OpenAsync().Wait();

    Console.WriteLine("Host open.  Hit enter to exit...");

    Console.Read();

    host.CloseAsync().Wait();
  }
}

public class GroupController : TestController { }
public class ContactsController : TestController { }
public class ContactController : TestController { }
public class GroupContactsController : TestController { }


public class TestController : ApiController
{
    public HttpResponseMessage Get()
    {
        var pathRouteData = (PathRouteData) Request.GetRouteData();

        var paramvalues = new StringBuilder();

        foreach (KeyValuePair<string, object> keyValuePair in pathRouteData.Values)
        {
            paramvalues.Append(keyValuePair.Key);
            paramvalues.Append(" = ");
            paramvalues.Append(keyValuePair.Value);
            paramvalues.Append(Environment.NewLine);
        }

        var url = pathRouteData.RootRouter.GetUrlForController(this.GetType());

        return new HttpResponseMessage()
                   {
                       Content = new StringContent("Response from " + this.GetType().Name + Environment.NewLine
                                                   + "Url: " + url.AbsoluteUri
                                                   + "Parameters: " + Environment.NewLine
                                                   + paramvalues.ToString())
                   };
    }
}

Unix- Microsoft.AspNet.WebApi.SelfHost Tavis.WebApiRouter . , , .

+3

All Articles