如何在asp.net mvc3中创建json servcie [英] how to create json servcie in asp.net mvc3

查看:104
本文介绍了如何在asp.net mvc3中创建json servcie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我是asp.net mvc3和Json的新手
如何在asp.net mvc3中创建json服务
是否可以从json服务连接ms sql数据库(在asp.net mvc3应用程序中具有)
我们可以在Iphone客户端上使用相同的json服务吗?
如果有人知道答案,请给我一个asp.net中的json示例应用程序mvc3

Hi I am new to asp.net mvc3 and Json
how to create json service in asp.net mvc3
Is it possible to connect ms sql database from json service (with in asp.net mvc3 app)
and can we use this same json service at Iphone client
If anybody knows the answer please give me a sample app for json in asp.net mvc3

推荐答案

您不需要在MVC中创建JSON服务.您只需要从控制器操作方法中返回JsonResult.通过控制器操作方法连接到sql数据库的方式与其他.net sql连接完全相同.您应该能够从其他应用程序重用Json的某些操作方法.

我不会给您提供示例应用程序,但是您可以看到制造商在文档和示例方面的功能:

www.asp.net/mvc/mvc3 [
You don''t need to create a JSON service in MVC. You just need to return a JsonResult from your controller action method. The ways of connecting to a sql database from a controller action method are exactly the same as other .net sql connections. You should be able to reuse some of the action methods for the Json from other applications.

I''m not going to give you a sample app, but you can see what the manufacturers have in the way of documentation and samples:

www.asp.net/mvc/mvc3[^]


jim lahey 所说,您不需要在MVC中创建JSON服务.您只需要从控制器操作方法中返回JsonResult即可."​​

下面的链接将向您展示如何:
使用模型优先和实体框架4.1构建MVC 3应用"
这可以帮助您,连接到sql server db throw Entity Framework

然后使Blog Controller包含下面的代码
As jim lahey said "You don''t need to create a JSON service in MVC. You just need to return a JsonResult from your controller action method"

The link below will show you how to :
"Building an MVC 3 App with Model First and Entity Framework 4.1"
That helps you, connect to sql server db throw Entity Framework

Then make a Blog Controller contains the code below
public class BlogController : Controller
{
   private BlogModelContainer db = new BlogModelContainer();

   public JsonResult Index()
   { 
      IList<blog> blogs = db.Blogs.ToList();
      return Json(blogs, JsonRequestBehavior.AllowGet);
   }
}


Blog 控制器(/Blog/Index/)调用 Index 操作后,您将获得以下内容:


After you call Index Action From Blog Controller (/Blog/Index/) you will get something like this:

[{"Id":1,"Title":"Title 1","BloggerName":"Blogger Name 1"},{"Id":2,"Title":"Title 2","BloggerName":"Blogger Name 2"},{"Id":3,"Title":"Title 3","BloggerName":"Blogger Name 3"},{"Id":5,"Title":"Title 5","BloggerName":"Blogger Name 5"},{"Id":7,"Title":"Title 7","BloggerName":"Blogger Name 7"},{"Id":8,"Title":"Title 8","BloggerName":"Blogger Name 8"},{"Id":10,"Title":"Title 1","BloggerName":"Blogger Name 10"}]


这篇关于如何在asp.net mvc3中创建json servcie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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