ASP.NET MVC使用JSON REST Webservics [英] ASP.NET MVC with JSON REST Webservics

查看:139
本文介绍了ASP.NET MVC使用JSON REST Webservics的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Java的背景是,但都被赋予了ASP.NET MVC RESTful服务项目上下工夫。我需要一些指导,从哪里开始。

I'm from Java background, but have been given a ASP.NET MVC RESTful services project to work on. I need some guidance as to where to start.

我有一个返回JSON数据的服务器上运行的Web服务。我已经建立一个MVC系统消耗数据,包括用户登录,帐户创建和基本的CRUD操作。

I have a web service running on the server that returns JSON data. I've to build a MVC system to consume the data including user login, account creation, and basic CRUD operations.

我很困惑如何模型连接到Web服务?

I'm confused about how to connect the model to the web service?

我怎么可以把它连接使用MVC来?我一定要使用网络API?我看过一些教程,但无济于事为止。对于教程任何建议?

How can I connect to it using MVC? DO I have to use Web API? I've watched few tutorials, but to no avail so far. Any suggestion for the tutorials?

推荐答案

我觉得这个教程将你在找什么。

I think this tutorial will do what you are looking for

<一个href=\"http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api\" rel=\"nofollow\">http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

在Web API,您有几种选择。上面的示例是做一个简单的方法。

with web apis you have a few options. the above sample is a simple way of doing it.

您也可以使用角JS或knockoutjs还是我最喜欢的是使用JS清风

you can also use Angular JS or knockoutjs or my favorite one is using breeze js.

添加有关如何将模型连接到Web服务的更多信息

Adding more information about how to connect model to the web service

在上面的例子中,在苏氨酸添加模块部分:

in the above example, in thr Adding a model section:

正在创建类产品:

    public class Product
{
    public int Id { get; set; }
    public string Name { get; set; }
    public string Category { get; set; }
    public decimal Price { get; set; }
}

这个类是我们的模型的蓝图

this class is the blue print of our model

我们填写控制器这个模型(控制器负责管理我们的模型,并将其介绍给视图)

we fill this model in the Controller ( Controller is responsible for managing our model and introduce it to the view)

所以,产品类被这里充满

So product class being filled here

 Product[] products = new Product[] 
    { 
        new Product { Id = 1, Name = "Tomato Soup", Category = "Groceries", Price = 1 }, 
        new Product { Id = 2, Name = "Yo-yo", Category = "Toys", Price = 3.75M }, 
        new Product { Id = 3, Name = "Hammer", Category = "Hardware", Price = 16.99M } 
    };

主要的一点是我们的控制器应ApiController继承。

The main point is our controller should be inherited from ApiController.

 public IEnumerable<Product> GetAllProducts()
    {
        return products;
    }

    public IHttpActionResult GetProduct(int id)
    {
        var product = products.FirstOrDefault((p) => p.Id == id);
        if (product == null)
        {
            return NotFound();
        }
        return Ok(product);
    }

以上两种方法都是从我们的模型中获取数据并返回数据。

The above two methods are getting data from our model and returning data.

,并在年底我们的JavaScript文件负责使用$ .getJSON读取数据。

and at the end our javascript file is responsible for reading the data by using $.getJSON .

所以要回答你的问题。如何连接模式的Web服务。答案是在控制器的方法。

so to answer your question. how connecting model to web services. the answer is in the methods in controllers.

这是一个非常简单的样本。在现实世界中的数据需要从数据库使用实体框架读

this is a very simple sample. in real-world data needs to be read from database with using entity framework

此示例显示了如何做到这一点。

this sample shows how to do it

<一个href=\"http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application\" rel=\"nofollow\">http://www.asp.net/mvc/overview/getting-started/getting-started-with-ef-using-mvc/creating-an-entity-framework-data-model-for-an-asp-net-mvc-application

这篇关于ASP.NET MVC使用JSON REST Webservics的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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