zoukankan      html  css  js  c++  java
  • [翻译]ASP.NET Web API 2入门

      原文: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>

      查看效果:

  • 相关阅读:
    修改MFC标题栏上的图标
    【转】子窗口刷新父窗口的问题
    水晶报表添加引用
    【转】MetadataType的使用,MVC的Model层数据验证
    poj 1556 The Doors 线段相交判断+最短路
    poj 1269 Intersecting Lines 求直线交点 判断直线平行共线
    string 函数操作
    poj 1066 Treasure Hunt 线段相交判断
    poj 1410 Intersection 线段相交判断
    poj 3347 Kadj Squares 扩大数据化整数
  • 原文地址:https://www.cnblogs.com/walden1024/p/4569138.html
Copyright © 2011-2022 走看看