【问题标题】:Wrap all ASP.net json responses / Return an anonymous object包装所有 ASP.net json 响应/返回一个匿名对象
【发布时间】:2014-09-12 12:56:13
【问题描述】:

我是 ASP.net(Visual Studio 2010,.NET 3.5)的新手,我想做以下事情:

我正在使用 OperationContracts 以 JSON 形式提供 Web 服务数据。一个用 angularJS 编写的移动应用正在使用这些 JSON 响应。

我希望每个 OperationContract 响应都是由标准响应对象包装的相关数据对象。

例如:

{
  error: false,
  error_detail: '',
  authenticated: false,
  data: { }
}

在数据变量中将包含每个请求类型所需的任何内容。

移动应用程序检查相关变量,如果一切正常,则将数据传递给任何请求它的对象(这部分正在工作并准备就绪)。

我知道它经常不受欢迎,但我希望基本上返回一个匿名对象,因为我可以轻松地构造一个匿名对象和我需要的任何数据,但似乎我被强行拒绝了这样做的能力。理想情况下,我不想在移动应用端添加另一层反序列化或其他东西,我希望在客户端做尽可能少的处理。

我可以很容易地使用我自己的测试 Web API 项目(请参阅下面的示例控制器)使其按要求工作,但不幸的是,我正在添加到现有项目中,而不是开始新项目。

谁能给点建议?

Web API 代码示例

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

        }

    }
}

【问题讨论】:

    标签: c# asp.net json angularjs


    【解决方案1】:

    由于您在客户端上使用 AngularJS,因此直接使用 JSON 响应,因此客户端上没有反序列化(或任何类似的东西)。您正在向您的客户端传递一个“javascript 对象”,AngularJS(或任何其他 JS 客户端)可以直接使用它。

    与匿名对象相比,使用类型化对象(带有简单的成员变量!)在服务器上没有序列化损失。 我个人更喜欢任何一天类型的对象。

    至于返回对象的结构,使用异常并让承诺链中的失败回调处理错误处理会更容易也更简洁。 IE。如果您在服务器端抛出异常,它将被以下内容捕获:

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

    令牌、身份验证信息等确实应该放在 http 标头而不是响应正文中。

    HTH,

    卡斯帕

    【讨论】:

    • 感谢 Casper,抱歉或者没有尽快回复。我曾希望我可以避免必须严格定义被送回的对象,但我发现情况并非如此,实际上也没什么大不了的。
    猜你喜欢
    • 2020-04-28
    • 1970-01-01
    • 1970-01-01
    • 2020-10-31
    • 1970-01-01
    • 2017-04-05
    • 2019-06-30
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多