Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.NET web api: documenting/specifying a service

I've been looking at asp.net Web Api, and I like the simplicity of implementing a practical web service.

However, how can I document/specify the interface of a service implemented like that? For example, is there any spec I can pass on or generate to a Java guy with no .NET background that will let him easily call and consume the service? What can I give to the javascript guy?

Ideally, I'd like the benefits of SOAP/XSD or something like it (easy to deserialize with nicely typed objects) for the java guy, while retaining a service that's callable from a web browser too (i.e. supports non-crufy JSON).

Update

It's worth noting that since I originally posted this question, I discovered ServiceStack which deals with this more naturally; supporting JSON, SOAP, and WSDL out of the box for the same service, as the client chooses. If you really want SOAP+JSON, it may be a better framework than ASP.NET Web Api.

like image 438
Eamon Nerbonne Avatar asked Aug 09 '12 10:08

Eamon Nerbonne


People also ask

How do I add a service to Web API?

You cannot add a service reference to a Web Api as it is just a framework that allows you to expose functionality over HTTP (basically you call a method on a controller).

What is FromBody and FromUri in Web API?

The [FromUri] attribute is prefixed to the parameter to specify that the value should be read from the URI of the request, and the [FromBody] attribute is used to specify that the value should be read from the body of the request.

What is service configuration in Web API?

Web API supports code based configuration. It cannot be configured in web. config file. We can configure Web API to customize the behaviour of Web API hosting infrastructure and components such as routes, formatters, filters, DependencyResolver, MessageHandlers, ParamterBindingRules, properties, services etc.


2 Answers

Update March 2016

It has been a while since this was answered and the tooling for documenting any Rest API has come along a lot. We are currently evaluating Swagger 2.0 now spawning out to the the Open Api Initiative, RAML and API Blueprint.

For WebAPI projects there is a tool Swashbuckle that auto creates Swagger (Open API) format documentation.

Format for documenting a REST service:

There are some attempts at structuring and standardising the description of REST services:

  • Web Application Desciption Language (WADL)
  • Web Service Description Language 2.0 (WSDL 2.0)

I think it is fair to say neither of the two approaches above have very wide adoption, but WADL does look like a nice concise format - a quick XSLT over the top and it could be a nice human readable format. There lots of examples of WADL for some famous API's at the apigee github site here.

When trying to find a documentation format that is appropriate I tend to look for "inspiration" from others.... Apigee do a lot of research in this area and have this as documentation for one of their API's here or take a look at Facebook's social graph api here.

The examples are largely in line with the advise here

How to auto document:

Using .NET: There is a good example of auto generating a WebApi "help" page here. A logical extension of this example may be to get it outing a WADL formated version as well...

Using Java: Jersey is a tool used in the Java community to generate WADL automatically.

What to share with the other developers:

Your Javascript guy will most likely want a manual like the Facebook and apigee one; giving the dev examples of the resources, urls, response codes etc. The most important thing here will be supporting JSON as the primary content type this will be the easiest for him/her to consume and work with by far.

Your Java guy would also want the manual, but also in theory they could be given example XSD for any XML representations of the resources you send/consume (assuming they make the request as "Content-Type: appplication/xml"). This may help them build proxy classes etc. JSON to Java and .NET converters are available online and given the example resources in your manual they should simply be able to use one of these types of services to quickly create proxies. Generate Java class from JSON?.

If you absolutely must have auto discovery, auto proxy generation etc then you may need to offer a choice of both REST and SOAP (with WSDL) endpoints - relevant question here: ReST Proxy Object Generator.

like image 136
Mark Jones Avatar answered Oct 20 '22 16:10

Mark Jones


You can use IApiExplorer interface and ApiExplorer class in order to create a help page for your Web Api service. This help page will describe the REST methods exposed by your service so any developer who understands how REST works will be able to use it (regardless the language). Please read below links for details and samples:

  • ASP.NET Web API: Introducing IApiExplorer/ApiExplorer
  • ASP.NET Web API: Generating a Web API help page using ApiExplorer
  • Documenting your ASP.Net Web API’s
like image 33
tpeczek Avatar answered Oct 20 '22 16:10

tpeczek