从外部服务器接收JSON,解析它并将其保存到与MVC2本地数据库 [英] Receive JSON from external server, parse it and save it to local database with MVC2

查看:313
本文介绍了从外部服务器接收JSON,解析它并将其保存到与MVC2本地数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要建立一个能够接收JSON一个MVC2项目({地址:Streetname,时代:42})这是从发送外部服务器,解析并保存到我的本地数据库(也许与模型的帮助?)。

I need to build a MVC2 project that can receive JSON ({"Address":"Streetname","Age":42"}) which is sent from an external server, parse it and save it to my local database (maybe with the help of a model?).

因为我从来没有这样做过,我不知道如何处理它。我需要哪些技术,它是在这种情况下,推荐一些指针(LINQ to SQL中,实体框架,ADO.NET实体框架)和如何接收JSON字符串(ActionMethod,或者在控制器?)和localY保存(自动上接收)。

As I have never done this before, I am unsure about how to handle it. I would need some pointers about which technique it is recommended in this case (Linq to sql, Entity Framework, ADO.NET Entity Framework) and how to receive the JSON string (ActionMethod, or maybe in the Controller?) and save it localy (automatically, on receive).

链接到正确的文件会更AP preciated,因为我没有很多的时间通过所有的初学者教程阅读。

Links to right documentation would be much appreciated, as I don't have a lot of time to read through all the beginners' tutorials.

推荐答案

如果你真的寻找一些超级简单,如果你使用MVC 3,其中已建成JSON模式控制器绑定也可以是因为这容易动作参数。

If you're really looking for something super simple, it can be as easy as this if you use MVC 3, which has built in JSON model binding for controller action parameters.

public class ContactController : Controller
{
    [HttpPost]
    public void SaveContact(Contact contact)
    {
        var context = new MyDataContext();
        context.Contacts.InsertOnSubmit(contact);
        context.SubmitChanges();
    }
}

我在这个例子中使用LinqToSql。除非你开始有域逻辑或更复杂的实体它真的你所需要的。

I'm using LinqToSql in this example. Unless you start having domain logic or more complex entities it's really all you need.

有关MVC 2,你需要下载期货库并添加到您的应用程序的启动。

For MVC 2, you need to download the Futures library and add this to your application startup.

ValueProviderFactories.Factories.Add(new JsonValueProviderFactory());

您可以找到详细信息<一个href=\"http://haacked.com/archive/2010/04/15/sending-json-to-an-asp-net-mvc-action-method-argument.aspx\"相对=nofollow>这里。

这篇关于从外部服务器接收JSON,解析它并将其保存到与MVC2本地数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆