WebAPI презентация в формате PowerPoint - скачать бесплатно

Скачать презентацию на тему: "WebAPI" с количеством слайдов в размере 26 страниц. У нас вы найдете презентацию на любую тему и для каждого класса школьной программы. Мы уверены, что наши слайды помогут найти вам свою аудиторию. Весь материал предоставлен бесплатно, в знак благодарности мы просим Вас поделиться ссылками в социальных сетях и по возможности добавьте наш сайт MirPpt.ru в закладки.

Нажмите для просмотра
WebAPI

2: In this tutorial, you will use ASP. NET Web API to create a web API that returns a list of products. The front-end web page uses jQuery to display the results.

3: Start Visual Studio and select New Project from the Start page. Or, from the File menu, select New and then Project. In the Templates pane, select Installed Templates and expand the Visual C node. Under Visual C, select Web. In the list of project templates, select ASP. NET Web Application. Name the project "ProductsApp" and click OK.

5: In the New ASP. NET Project dialog, select the Empty template. Under "Add folders and core references for", check Web API. Click OK.

7: Adding a Model A model is an object that represents the data in your application. ASP. NET Web API can automatically serialize your model to JSON, XML, or some other format, and then write the serialized data into the body of the HTTP response message. As long as a client can read the serialization format, it can deserialize the object. Most clients can parse either XML or JSON. Moreover, the client can indicate which format it wants by setting the Accept header in the HTTP request message. Lets start by creating a simple model that represents a product. If Solution Explorer is not already visible, click the View menu and select Solution Explorer. In Solution Explorer, right-click the Models folder. From the context menu, select Add then select Class.

9: Name the class "Product". Add the following properties to the Product class. namespace ProductsApp. Models public class Product public int Id get; set; public string Name get; set; public string Category get; set; public decimal Price get; set;

10: Adding a Controller In Web API, a controller is an object that handles HTTP requests. Well add a controller that can return either a list of products or a single product specified by ID. In Solution Explorer, right-click the Controllers folder. Select Add and then select Controller.

12: In the Add Scaffold dialog, select Web API Controller - Empty. Click Add. (лучше – with r/w action)

13: In the Add Controller dialog, name the controller "ProductsController". Click Add.

14: The scaffolding creates a file named ProductsController. cs in the Controllers folder.

15: If this file is not open already, double-click the file to open it. Replace the code in this file with the following: 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 GetAllProducts() return products; public IHttpActionResult GetProduct(int id) var product products. FirstOrDefault((p) p. Id id); if (product null) return NotFound(); return Ok(product);

16: Вы делаете: 1 – тестовый массив данных 2 – метод, который ищет элемент массива по его Id 3 – метод, который находит все элементы массива Далее эту информацию может запросить кто угодно (при условии, что она развернута на сервере и сервер корректно работает)

17: Thats it! You have a working web API. Each method on the controller corresponds to one or more URIs: Controller MethodURI GetAllProducts/api/products GetProduct/api/products/id For the GetProduct method, the id in the URI is a placeholder. For example, to get the product with ID of 5, the URI is api/products/5.

18: Calling the Web API with Javascript and jQuery In this section, well add an HTML page that uses AJAX to call the web API. Well use jQuery to make the AJAX calls and also to update the page with the results.

19: In Solution Explorer, right-click the project and select Add, then select New Item.

20: In the Add New Item dialog, select the Web node under Visual C, and then select the HTML Page item. Name the page "index. html".

21: Полный код по ссылке https://github. com/aspnet/Docs/blob/master/aspnet/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api/samples/sample3. html

22: Получение всего списка данных $(document). ready(function () // Send an AJAX request $. getJSON(apiUrl) . done(function (data) // On success, data contains a list of products. $. each(data, function (key, item) // Add a list item for the product. $(, text: formatItem(item) ). appendTo($(products)); ); ); );

23: Поиск по Id function find() var id $(prodId). val(); $. getJSON(apiUrl / id) . done(function (data) $(product). text(formatItem(data)); ) . fail(function (jqXHR, textStatus, err) $(product). text(Error: err); );

24: Основное Страница обращается к сервису по адресу … Функция поиска запрашивает по этому адресу данные getJSON Эти данные приходят по протоколу TCP/IP Собранные данные трактуются как данные в формате JSON «Распознанные» данные выводятся на страницу

25: Running the Application Press F5 to start debugging the application.

26: Using F12 to View the HTTP Request and Response Запустите в браузере средства разработчика клавишей F12

Скачать презентацию


MirPpt.ru