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?
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.
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.
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.
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:
Cons:
WCF
Pros:
Cons:
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)
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With