ASP.NET MVC Web API
Feb 25, 2021 09:07 0 Comments ASP.net MVC PARTH

                   ASP.NET MVC Web API

A Web API is an application programming interface for the Web. Web APIs are a feature for getting and saving data in different formats and are light weight when compared to WCF.

 

What is ASP.NET Web API?

Framework for building and consuming HTTP services that can reach a broad range of

clients including browsers, phones and tablets. We Use XML or JSON or other formats with API.

A simple example can be: Calling an API from jQuery and better utilizing the client's machine and browser.

 

Why ASP.NET MVC Web API?

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.

 

Features of ASP.NET Web API

  • Modern HTTP programming model

Directly 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.

  • Filters

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

  • Content negotiation

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

from an API.

Provide default support for XML, JSON, and Form URL-encoded formats, and extend this support by adding your own formatters

Model 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 details

Rather than setting HTTP details in static context objects, Web API actions can now work with

instances of HttpRequestMessage and HttpResponseMessage.

Generic versions of these objects also exist to work with custom types in addition to the HTTP types.

  • Query composition

By simply returning IQueryable, 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-host

Web 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 Design

 

                                       

  • The client is whatever consumes the web API (browser, mobile app, etc.)
  • 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 MVC Web API

MVC : Model -> View -> Controller

1. Create a Model

2. Create a Controller

 

Routing Table

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

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 server.
  • PUT updates a resource at a specified URI.
  • POST Create a new resource. The server assigns the URI for the new object and returns this URI as part of the response message.
  • DELETE deletes a resource at a specified URI.

 

ASP.NET MVC Web API – Example

An example of an API for crud operation:

 

 

Prev Next
About the Author
Topic Replies (0)
Leave a Reply
Guest User

You might also like

Not sure what course is right for you?

Choose the right course for you.
Get the help of our experts and find a course that best suits your needs.


Let`s Connect