Using ASP.Net WebAPI build REST services (a) - simple example

Since the granting of REST Web services is very simple to use, it has increasingly become the preferred method of enterprise back-end services integration. This article describes here how to quickly build a REST-ful service through Microsoft's Asp.Net WebAPI.

First create a Asp.Net Web application (here I use the Visual Studio 2013, it has built-in Web API2).

    

Select Empty (empty project) in the template out and tick WebAPI. When you click OK, you create an empty WebAPI service.

    

At this time, only an empty project, does not have any function, before proceeding to the next First we look at the basic operation of the REST model, can be roughly divided into the following four categories:

  • POST - Create a resource
  • GET - retrieve resources
  • PUT - update the resource
  • DELETE - delete the resource

Very classic CRUD model. Implement such a model in the Web API is very simple, direct use wizard to build a Controller

    

    

If the traditional wizard, remember that 1 behind the Wizard to remove:

 

The default template reads as follows:

    public class ValuesController : ApiController
    {
        // GET api/<controller>
        public IEnumerable<string> Get()
        {
            return new string[] { "value1", "value2" };
        }

        // GET api/<controller>/5
        public string Get(int id)
        {
            return "value";
        }

        // POST api/<controller>
        public void Post([FromBody]string value)
        {
        }

        // PUT api/<controller>/5
        public void Put(int id, [FromBody]string value)
        {
        }

        // DELETE api/<controller>/5
        public void Delete(int id)
        {
        }
    }

这其实已经帮我们实现了一个最基本的服务了,不过这个服务中只实现了Get,它支持如下两种中方式的URL访问(其它的方式也能访问,但没有具体的效果):

  • api/values        访问所有的Value列表    
  • api/values/{id}        根据ID访问Value

按Ctrl + F5中执行,在浏览器中输入相应的地址即可看到结果

    

下面我们要做的就是完善它,实现一个简单的查询功能,这里我引用了微软官方的一个例子:

public class ProductsController : ApiController
{
        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 }
        };

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

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

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

此时,我们就可以在浏览器中看到结果了(由于Controller改名字了,此时的地址就变成了api/products)

    

到此为止,一个基于Asp.net Web API的 简单的REST Web服务就构建完成了,由于篇幅所限,这里就不做更多的介绍了,跟多信息可以参看微软官方文档:Getting Started with ASP.NET Web API 2。另外,如果想对REST有更深入的了解的话,可以看看infoq的这篇文章:深入浅出REST。关于Asp.net Web API其它内容,我后续大概还会陆续写几篇文章来介绍它。

Guess you like

Origin www.cnblogs.com/blogsaspx/p/11093854.html