找不到为什么我的 return 被序列化两次

Cant find why my return is being serialized twice

我正在从我的 Angular 应用程序调用自定义 Web API,我需要 JSON.parse() 我的响应两次才能访问属性。我不确定为什么会这样。

    /// <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());
        }
    }

这是我的 API 中的方法,下面是我在 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;

}]);

如有任何帮助,我们将不胜感激。

    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 }
        );
    }
}

以上是我的 WebApiConfig.cs 代码。

这个

return Ok(JsonConvert.SerializeObject(pl));

框架将序列化为您传递的值,但您在将其传递给操作结果之前也使用 JsonConvert.SerializeObject 对其进行序列化,因此是双重序列化。

只需将值传回

/// <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());
    }
}

让框架做它的事情。