在Asp.Net MVC 2和Asp.Net服务器端C#代码之间共享Json数据?

时间:2021-10-26 15:53:44

I created and love my Asp.Net MVC2 application. It's a very nice DDD app with Domain Model classes, View Model classes, a repository, and Json action methods to expose data.

我创建并喜欢我的Asp.Net MVC2应用程序。这是一个非常好的DDD应用程序,包含域模型类,视图模型类,存储库和用于公开数据的Json操作方法。

My coworker wants to share my data with his Asp.Net Forms based C# code. He wants to pull through the Internet a class definition (like a Data Contract), then fill it with my Json results, effectively using something like a remote repository.

我的同事希望与他的基于Asp.Net Forms的C#代码共享我的数据。他希望通过Internet获取类定义(如数据协定),然后使用我的Json结果填充它,有效地使用类似远程存储库的东西。

Any links or ideas on how to provide him with data contracts and data?

关于如何向他提供数据合同和数据的任何链接或想法?

Darin Dimitrov had an excellent idea of consuming JSON data using data contracts here. Just wondering if it's possible to use MVC as the source for these items, then let him create the objects on his side, filled with data from my side.

Darin Dimitrov非常了解如何使用数据合同来使用JSON数据。只是想知道是否可以使用MVC作为这些项目的来源,然后让他创建他身边的对象,填充我身边的数据。

The key to this question is how to send him my data classes, then send him my data.

这个问题的关键是如何向他发送我的数据类,然后将他的数据发送给他。

class Program
{
    [DataContract]
    class Person
    {
        [DataMember(Name = "name")]
        public string Name { get; set; }
        [DataMember(Name = "surname")]
        public string Surname { get; set; }
        [DataMember(Name="age")]
        public int Age { get; set; }
    }

    static void Main(string[] args)
    {
        var json = @"{""name"" : ""michael"", ""surname"" : ""brown"", ""age"" : ""35""}";

        var serializer = new DataContractJsonSerializer(typeof(Person));
        using (var stream = new MemoryStream(Encoding.UTF8.GetBytes(json)))
        {
            var person = (Person)serializer.ReadObject(stream);
            Console.WriteLine("Name : {0}, Surname : {1}, Age : {2}", 
                person.Name, person.Surname, person.Age);
        }
    }
}

1 个解决方案

#1


1  

Write an OData service. The format is JSON, but the tools to consume it easily -- from many languages -- are already written for you.

写一个OData服务。格式是JSON,但是很容易从多种语言中使用它的工具已经为您编写了。

The nice thing about this is that your data is now not only consumable by your JS and your friend's ASP.NET app, it's consumable by Excel, PHP, etc.

关于这一点的好处是,您的数据现在不仅可以由您的JS和您朋友的ASP.NET应用程序使用,它可以通过Excel,PHP等消费。

#1


1  

Write an OData service. The format is JSON, but the tools to consume it easily -- from many languages -- are already written for you.

写一个OData服务。格式是JSON,但是很容易从多种语言中使用它的工具已经为您编写了。

The nice thing about this is that your data is now not only consumable by your JS and your friend's ASP.NET app, it's consumable by Excel, PHP, etc.

关于这一点的好处是,您的数据现在不仅可以由您的JS和您朋友的ASP.NET应用程序使用,它可以通过Excel,PHP等消费。