How to Route non-CRUD actions in a RESTful ASP.NET Web API?

前端 未结 3 1643
自闭症患者
自闭症患者 2020-12-20 12:11

I am trying to design a RESTful web API for our service using ASP.NET Web API. I\'m running into trouble with figuring out how to route non-CRUD actions to the proper contr

3条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-12-20 12:40

    To handle the lock/unlock scenario you could consider adding a State property to the Door object:

       public State State { get; set; }
    

    where State is an enum of available values, e.g.

    {
    LockedFromOutsideRoom,
    LockedFromInsideRoom,
    Open
    }
    

    To clarify: That you're adding a state to the object is not against restful principles as the state is passed over the api every time you make a call to do something with the Door.

    Then via the api you would send a PUT/POST request to change the state of the Door on each lock/unlock. Post would probably be better as it's only one property that gets updated:

    POST: http://api.contoso.com/v1/doors/1234/state
    body: {"State":"LockedFromInsideRoom"}
    

提交回复
热议问题