Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net options for JSON API?

I need to create a .Net api that will return JSON that will be used by mobile applications.

One approach is to just use an MVC app and have my controller return JSON, so going to a url.com/controller/action/params would give me my JSON.

I've heard that creating a WCF Service is also a good choice. I don't know much at all about WCF, though.

Are there pros and cons to each one? Is one more reliable to be used as a service that returns JSON only?

like image 690
RJP Avatar asked May 03 '12 03:05

RJP


People also ask

How do I post JSON data to API using C#?

To post JSON to a REST API endpoint using C#/. NET, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the C#/. NET POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.

Can ASP Net Web API specialize to XML or JSON?

Web API provides media-type formatters for both JSON and XML. The framework inserts these formatters into the pipeline by default. Clients can request either JSON or XML in the Accept header of the HTTP request.

Is there any standard for JSON:API response format?

Yes there are a couple of standards (albeit some liberties on the definition of standard) that have emerged: JSON API - JSON API covers creating and updating resources as well, not just responses.


3 Answers

Another contender is ASP.NET Web API which uses WCF in self hosted scenario.

There are pros and cons but it all depends what you need now vs. latter, what is your level of expertise, technology commitment and what are the design trade-offs.

It depends what you mean by reliable. One technology is not necessarily more or less reliable. There are many factors that go into reliability.

These are some of the few pros/cons in no particular order, preference or completeness.

ASP.Net MVC / WebApi / ServiceStack

Pros:

  • Setup and running within minutes for basic scenario (have URL get some JSON data)
  • Simple to configure.
  • REST setup straight forward.
  • Complete control over routing.
  • JSON native support (ASP.NET Web API can automatically serialize
    your model to JSON, XML, or some other format, and then write
    the serialized data into the body of the HTTP response
    message.)

Cons:

  • Cannot describe your service to a consumer: no api like WSDL exist as of yet that can tell the client data types, operations and service requirements
  • Transport security only - point-to-point security
  • No message level security
  • No Service Discovery protocols (as of now)
  • No Message routing
  • No multi-protocol support e.g. tcp
  • Single hosting scenario (IIS - this can be a pro too)

WCF

Pros:

  • Multi-protocol support
  • Transport and Message security
  • Highly configurable and inter-operable
  • Very extensible
  • Supports various messaging scenarios e.g. routing, duplex, pub/sub, queuing, etc.
  • Lots of knobs for shaping messages and internal workings
  • Wide variety of hosting scenarios (IIS/WAS, Windows Service, Console)

Cons:

  • Steep learning curve
  • REST story weak (yes webHttpBinding exists but try explaining to someone TemplateURI and WebInvoke/Web get and BodyStyle)
  • Lots of knobs
like image 121
Petar Vučetin Avatar answered Oct 20 '22 13:10

Petar Vučetin


If all you are looking for is a service, then I would suggest something like WCF. However, WCF is cumbersome, so I would suggest something simpler like ServiceStack. It allows you to create your service using basic POCOs. It also comes built in and ready to respond with JSON/XML/SOAP (no extra actions on your part)

like image 42
Justin Pihony Avatar answered Oct 20 '22 13:10

Justin Pihony


I would really go with the WCF approach. It will give you more flexibility and allow you to run the service using many different protocols, not only HTTP, for example.

like image 33
Icarus Avatar answered Oct 20 '22 13:10

Icarus