[翻譯]ASP.NET Web API 2入門

來源:互聯網
上載者:User

標籤:

  原文:Getting Started with ASP.NET Web API 2

 

  Step 1:建立一個Empty的Web API Project。

  Step 2:添加一個Model:

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

  Step 3:添加一個Empty的Web API Controller:

    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> GetAllProducts()        {            return products;        }        public IHttpActionResult GetProduct(int id)        {            var product = products.FirstOrDefault((p) => p.Id == id);            if (product == null)            {                return NotFound();            }            return Ok(product);        }    }

  至此我們已經完成一個Web API。建立一個HTML Page調用此Web API:

<!DOCTYPE html><html xmlns="http://www.w3.org/1999/xhtml"><head>    <title>Product App</title></head><body>    <div>        <h2>All Products</h2>        <ul id="products" />    </div>    <div>        <h2>Search by ID</h2>        <input type="text" id="prodId" size="5" />        <input type="button" value="Search" onclick="find();" />        <p id="product" />    </div>    <script src="http://ajax.aspnetcdn.com/ajax/jQuery/jquery-2.0.3.min.js"></script>    <script>        var uri = ‘api/products‘;        $(document).ready(function () {            // Send an AJAX request            $.getJSON(uri)                .done(function (data) {                    // On success, ‘data‘ contains a list of products.                    $.each(data, function (key, item) {                        // Add a list item for the product.                        $(‘<li>‘, { text: formatItem(item) }).appendTo($(‘#products‘));                    });                });        });        function formatItem(item) {            return item.Name + ‘: $‘ + item.Price;        }        function find() {            var id = $(‘#prodId‘).val();            $.getJSON(uri + ‘/‘ + id)                .done(function (data) {                    $(‘#product‘).text(formatItem(data));                })                .fail(function (jqXHR, textStatus, err) {                    $(‘#product‘).text(‘Error: ‘ + err);                });        }    </script></body></html>

  查看效果:

 

[翻譯]ASP.NET Web API 2入門

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.