I'm new to ASP.net (Visual Studio 2010, .NET 3.5) and I would like to do the following:
I am using OperationContracts to provide webservice data as JSON. A mobile app written in angularJS is consuming these JSON responses.
I would like each OperationContract response to be the relevant data object wrapped by a standard response object.
e.g:
{
error: false,
error_detail: '',
authenticated: false,
data: { }
}
Within the data variable will be whatever is required of each individual request type.
The mobile app checks the relevant variables and if all is OK, passes on the data to whatever requested it (this part is working and ready).
I know it's often frowned upon, but I had hoped to basically return an anonymous object because I can easily construct an anonymous object and stuff in whatever data I require, but it seems I am forcefully denied the ability to do this. Ideally I do not want to add another layer of de-serialization or something on the mobile app side, I want to do as little processing as possible client side.
I was very easily able to get this working as required with my own test Web API project (see example controller below), but unfortunately I am adding to an existing project, not starting a new one.
Can anyone offer any advice?
Example Web API Code
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace tut3.Controllers
{
public class ValuesController : ApiController
{
/**
* Take the provided dataResponse object and embed it into
* the data variable of the default response object
**/
private object Response(object dataResponse)
{
return new
{
success = false,
error = "",
error_detail = "",
authenticated = false,
token = "",
token_expiry = 0,
data = dataResponse
};
}
/**
* This could be a normal web service that uses the Knadel database etc etc, the only difference is
* the return is sent through the Response() function
**/
public object Get()
{
object[] local = new[] {
new { cat = "cat", dog = "dog" },
new { cat = "cat", dog = "dog" },
new { cat = "cat", dog = "dog" },
new { cat = "cat", dog = "dog" },
new { cat = "cat", dog = "dog" }
};
/**
* Pass local to Response(), embed it in data and then return the whole thing
**/
return Response(local);
}
}
}
Since you're using AngularJS on the client and therefore directly consuming the JSON response there is no de-serialization (or anything resembling that) on the client. You are passing your client a "javascript object" which can be directly used by AngularJS (or any other JS client).
There is no serialization penalty on the server by using a typed object (with simple member variables!) compared to the anonymous object. I would personally prefer a typed object any day.
As to the structure of your return object it would be easier and somewhat cleaner to use exceptions and let the failure callback in the promise chain take care of errorhandling. I.e. if you throw an exception serverside it will be caught by something like this:
$http.get('yourServerUrl').then(function successCallback(result){
// Everything went well, display the data or whatever
}, function errorCallback(error){
// Something went wrong,
// the error object will contain statusCode and the message from the exception
});
tokens, authentication information etc. should really go in the http header rather than in the response body.
HTH,
Casper