Professional Documents
Culture Documents
0
By
Amareswara Rao
Implement a Web API
Content negotiation is the process by which a response
format is selected for a given response when there are
multiple representations available. Content negotiation
can happen multiple ways, but the primary way it
happens is via the HttpRequest header
Simple types are filled with data from your URL. This
means that you can pass a property such as id by putting it
in the URL and making sure that routing knows about it.
http://localhost/api/Persons/?FirstName=John&LastName=Doe
FromBody
http://localhost/api/Persons/
Using HttpMessageHandler
Message handler : An incoming request is matched to a
route and then sent to a specific Web API controller.
config.MessageHandlers.Add(new LoggingMessageHandler());
Open/closed principle
Why?
But how do you get this class into your Web API controller? You
could add a constructor like this:
But this still couples your class to a concrete
implementation, although in a dierent place. Instead,
you can use dependency injection, meaning that you ask
for the objects you need, and they are injected into your
class at construction. You do this by first changing your
constructor:
To help the Web API framework, create the class and resolve all
dependencies you need to implement an interface called
IDependencyResolver. This class is used by Web API to create
objects that have dependencies, such as the ContentController.
config.DependencyResolver = new ResolveController();