+ All Categories
Home > Software > Web API with ASP.NET MVC by Software development company in india

Web API with ASP.NET MVC by Software development company in india

Date post: 16-Apr-2017
Category:
Upload: ifour-institute-sustainable-learning
View: 85 times
Download: 0 times
Share this document with a friend
19
iFour Consultancy ASP.NET MVC Web API
Transcript
Page 1: Web API with ASP.NET  MVC  by Software development company in india

iFour Consultancy

ASP.NET MVC Web API

Page 2: Web API with ASP.NET  MVC  by Software development company in india

ASP.NET Web APIWhy ASP.NET MVC Web API?ASP.NET Web API FeaturesASP.NET Web API DesignASP.NET MVC Web APIRouting TableError & Exception HandlingModel Validation

INDEX

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 3: Web API with ASP.NET  MVC  by Software development company in india

Framework for building and consuming HTTP services that can reach a broad range of clients including browsers, phones and tablets

Use XML or JSON or something else with API. JSON is nice for mobile apps with slow connections

For example: Call an API from jQuery and better utilize the client's machine and browser

What is ASP.NET Web API?

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 4: Web API with ASP.NET  MVC  by Software development company in india

It is built for all the other, non-human interactions website or service needs to support. Think about jQuery code that's making an Ajax request, or a service interface that supports a mobile client

In these cases, the requests are coming from code and expect some kind of structured data and specific HTTP Status Codes

These two are very complimentary, but different enough that trying to build out HTTP services using ASP.NET MVC took a lot of work to get right

Why ASP.NET MVC Web API?

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 5: Web API with ASP.NET  MVC  by Software development company in india

Modern HTTP programming modelDirectly access and manipulate HTTP requests and responses in Web APIs using a new, strongly

typed HTTP object model Full support for routes

Web APIs now support the full set of route capabilities that have always been a part of the Web stack, including route parameters and constraints

Additionally, mapping to actions has full support for conventions, so no longer need to apply attributes such as [HttpPost] to your classes and methods

FiltersWeb APIs now supports filters, including well-known filters such as the [Authorize] attribute

ASP.NET Web API Features

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 6: Web API with ASP.NET  MVC  by Software development company in india

Content negotiationThe client and server can work together to determine the right format for data being returned

from an APIProvide default support for XML, JSON, and Form URL-encoded formats, and extend this support

by adding your own formattersModel binding and validation: Model binders provide an easy way to extract data from various

parts of an HTTP request and convert those message parts into .NET objects which can be used by the Web API actions

Improved testability of HTTP detailsRather than setting HTTP details in static context objects, Web API actions can now work with

instances of HttpRequestMessage and HttpResponseMessageGeneric versions of these objects also exist to work with custom types in addition to the HTTP

types

ASP.NET Web API Features (Cont.)

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 7: Web API with ASP.NET  MVC  by Software development company in india

Query compositionBy simply returning IQueryable<t>, Web API will support querying via the OData URL

conventions Improved Inversion of Control (IoC) via DependencyResolver

Web API now uses the service locator pattern implemented by MVC’s dependency resolver to obtain instances for many different facilities

Code-based configuration Web API configuration is accomplished solely through code, leaving config files clean

Self-hostWeb APIs can be hosted in own process in addition to IIS while still using the full power of routes

and other features of Web API

ASP.NET Web API Features (Cont.)

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 8: Web API with ASP.NET  MVC  by Software development company in india

The client is whatever consumes the web API (browser, mobile app, and so forth) A model is an object that represents the data in application. In this case, the only model is a to-

do item. Models are represented as simple C# classes (POCOs) A controller is an object that handles HTTP requests and creates the HTTP response

ASP.NET Web API Design

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 9: Web API with ASP.NET  MVC  by Software development company in india

MVC : Model -> View -> Controller Create a Model

Create a Controller

ASP.NET MVC Web API

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 10: Web API with ASP.NET  MVC  by Software development company in india

It is defined, by default, as api/{controller}/{id} where action is defined by an HTTP method (in global.asax Application_Start method)

Routing Table

HTTP Method URI Path Action Performed

GET /api/products Get All Products

GET /api/products/id Get Product by Id

GET /api/products?category=category Get Products by Category

POST /api/products Insert Product

PUT /api/products/id Update Product

DELETE /api/products/id Delete Product

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 11: Web API with ASP.NET  MVC  by Software development company in india

The four main HTTP methods are mapped to CRUD operations:Get retrieves the representation of the resource at a specified URI. GET should have no side

effects on serverPUT updates a resource at a specified URIPOST Create a new resource. The server assigns the URI for the new object and returns this URI

as part of the response messageDELETE deletes a resource at a specified URI

Routing Table

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 12: Web API with ASP.NET  MVC  by Software development company in india

ASP.NET MVC Web API - Example

APIs for crud operation for products

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 13: Web API with ASP.NET  MVC  by Software development company in india

Response messages, errors and exceptions are translated to HTTP response status codesFor example:

POST request should reply with HTTP status 201 (created) DELETE request should reply with HTTP status 204 (no content)

But: If a PUT/GET request is done with an invalid id, it should reply with HTTP status 404 (not found)

Or if a PUT request has invalid model, it can reply with HTTP status 400 (bad request)

Error & Exception Handling

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 14: Web API with ASP.NET  MVC  by Software development company in india

By default, all .NET exceptions are translated into an HTTP response with status code 500 (internal error)

It is possible to register Exception filters:

Error & Exception Handling (Cont.)

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 15: Web API with ASP.NET  MVC  by Software development company in india

Like MVC, Web API supports Data Annotations (System.ComponentModel.DataAnnotations)

Model Validation

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 16: Web API with ASP.NET  MVC  by Software development company in india

Model Validation - Example

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 17: Web API with ASP.NET  MVC  by Software development company in india

Create a FilterAttribute

Add it to Filters in Global.asax Application_Start

Model Validation - FilterAttribute

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 18: Web API with ASP.NET  MVC  by Software development company in india

https://www.tutorialspoint.com/asp.net_mvc/asp.net_mvc_web_api.htm https://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-

your-first-web-api http://www.c-sharpcorner.com/uploadfile/4d9083/how-to-create-web-api-in-asp-net-

mvc/

References

http://www.ifourtechnolab.com/

C# Software Development Companies India

Page 19: Web API with ASP.NET  MVC  by Software development company in india

Questions?

http://www.ifourtechnolab.com/

C# Software Development Companies India


Recommended