I am calling a custom Web API from my Angular app, and I need to JSON.parse() my response twice in order to access the properties. I am not sure why this is happening.
/// <summary>
/// Gets list of printers
/// </summary>
[HttpGet]
public IHttpActionResult GetPrinterList()
{
try
{
List<Printer> pl = new List<Printer>();
// List the print server's queues
PrintQueueCollection myPrintQueues = new PrintServer(@"\\LPH-Printers").GetPrintQueues();
foreach (PrintQueue pq in myPrintQueues)
{
Printer p = new Printer();
p.Name = pq.FullName;
pl.Add(p);
}
return Ok(JsonConvert.SerializeObject(pl));
}
catch (Exception e)
{
return BadRequest(e.ToString());
}
}
This is the method in my API, and below is how I am calling it in Angular
'use strict';
app.factory('printerService', ['$http', 'ngAuthSettings', function ($http, ngAuthSettings) {
var serviceBase = ngAuthSettings.apiServiceBaseUri;
var printerServiceFactory = {};
var _DefaultPrinter = function (val) {
return $http.get(serviceBase + 'api/LibertyMobile/GetUserDefaultPrinter', {
params: { 'username': val }
})
};
var _SetDefaultPrinter = function (userName, DefaultPrinter) {
return $http({
url: serviceBase + "api/LibertyMobile/SaveUserDefaultPrinter",
method: "POST",
params: { 'username': userName, 'printer': DefaultPrinter }
});
}
var _GetPrinterList = function () {
return $http.get(serviceBase + 'api/LibertyMobile/GetPrinterList');
}
printerServiceFactory.DefaultPrinter = _DefaultPrinter;
printerServiceFactory.SetDefaultPrinter = _SetDefaultPrinter;
printerServiceFactory.GetPrinterList = _GetPrinterList;
return printerServiceFactory;
}]);
Any help would be greatly appreciated.
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Configure Web API to use only bearer token authentication.
config.SuppressDefaultHostAuthentication();
config.Filters.Add(new HostAuthenticationFilter(OAuthDefaults.AuthenticationType));
// Web API routes
config.MapHttpAttributeRoutes();
//config.Routes.MapHttpRoute(
// name: "DefaultApi",
// routeTemplate: "api/{controller}/{id}",
// defaults: new { id = RouteParameter.Optional }
//);
//config.Routes.MapHttpRoute(
// name: "GetPartNumbers",
// routeTemplate: "api/Inventory/GetPartNumbers/{partnum}/{user}",
// defaults: new { controller = "Inventory" }
//);
config.Routes.MapHttpRoute(
name: "ApiByAction",
routeTemplate: "api/{controller}/{action}",
defaults: new { controller = "Inventory", action = RouteParameter.Optional }
);
}
}
Above is my WebApiConfig.cs code.
This
return Ok(JsonConvert.SerializeObject(pl));
The framework will serialize the value passed for you but you are also serializing it using JsonConvert.SerializeObject
before passing it to the action result, hence the double serialization.
Just pass the value back
/// <summary>
/// Gets list of printers
/// </summary>
[HttpGet]
public IHttpActionResult GetPrinterList() {
try {
List<Printer> pl = new List<Printer>();
// List the print server's queues
PrintQueueCollection myPrintQueues = new PrintServer(@"\\LPH-Printers").GetPrintQueues();
foreach (PrintQueue pq in myPrintQueues) {
Printer p = new Printer();
p.Name = pq.FullName;
pl.Add(p);
}
return Ok(pl);
} catch (Exception e) {
return BadRequest(e.ToString());
}
}
And let the framework do its thing.