开发者

Receive JSON from external server, parse it and save it to local database with MVC2

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?).

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, A开发者_开发知识库DO.NET Entity Framework) and how to receive the JSON string (ActionMethod, or maybe in the Controller?) and save it localy (automatically, on receive).

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


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

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

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

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

You can find details here.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜