How Do Web Services Integrate With Existing Api
This article explains what the Web API is and its basics.
Permit's start with a definition kickoff.
Web API
The ASP.NET Spider web API is a framework that makes it easy to build HTTP services that reach a broad range of clients, including browsers and mobile devices.
The ASP.Cyberspace Spider web API is an ideal platform for building Restful applications on the .Internet Framework. Referred from "Microsoft.com".
Why to use the Web API
Currently most mobile devices, browsers and tablets are the medium for accessing nigh of the net and in this also people are using mobile apps the most and to provide data to apps we are now going to use the Microsoft new technology called Spider web API.
When to use information technology
If you desire to expose the data/information of your awarding to your clients and other people so that other people can use your data and interact with the data/information yous expose to them.
For example, a mobile application requires a service.
HTML 5 requires a service.
Desktop PC and tablets crave services.
Currently near device apps require Web API services.
The ASP.Net Framework leverages both spider web standards such as HTTP, JSON and XML and it provides a simple way to build and betrayal Remainder based data services.
Some core concepts of ASP.Cyberspace MVC are like to the ASP.Net Web API such every bit routing and controllers.
Requirements
Nosotros are using Visual Studio 2012 for a demo application.
Building a Web API
Let's get-go with creating a Spider web API project.
Get-go Visual Studio and select New project from the Start page or from the File menu select "File" -> "New" -> "Project...".
In the template pane select Installed Templates and expand the Visual C# carte du jour. Inside that Visual C# select Web. In the listing of projects select ASP.Net MVC 4 Web Application.
And name the project WebAPI_Basic.
For reference encounter the following snapshot.
After calculation, a new dialog will pop-up.
Inside the project template select Spider web API and in the view engine select Razor.
A new projection is created now.
Let'due south brainstorm with adding Web API Controller
At present let's begin with adding a Web API Controller. It is nearly similar to calculation a Controller in ASP.Cyberspace MVC.
Right-click on the Controller binder and add a new Web API Controller with the name CarDetailsController and in the template select API Controller with an empty read / write action.
After adding the Controller you volition see the lawmaking as in the following snapshot.
You can go on this Spider web API controller anywhere in the application.
If you want to follow the convention so create the new folder in the root your of application with the name API.
Inside that yous tin can add a Spider web API controller.
You lot take successfully added a Web API controller to your application.
Now you tin can run the application and test it.
For testing I am passing http://localhost:32359/api/cardetails/one for calling the method get.
Wow, it's working!
It's piece of cake to configure it equally a Web API.
The ASP.Net MVC and ASP.Net Web API makes heavy use of convention for configuration to lighten the work load for creating the services.
For case, add a decorating method with attributes to make it easy to do Grime operations.
Else information technology will brand information technology hard to understand and lawmaking.
- [HttpPut]
- public void Put( int id, [FromBody] string value)
- {
- }
- [HttpPost]
- public void Post([FromBody] string value)
- {
- }
- [HttpDelete]
- public void Delete( int id)
- {}
The HTTP actions and their corresponding Grime operations are:
- Go (Read)
Retrieves the representation of the resource. - PUT(Update)
Update an existing resource. - POST (Create)
Create new resource. - DELETE (Delete)
Delete an existing resources.
Now allow'due south begin with how to create a Crud operation with the Spider web API.
Permit'south start by adding a Model.
To add the model right-click on the model binder and add a form with the name CarsStock.
After adding the Model CarsStock.cs now permit'south beginning with adding properties to the course.
Afterwards adding the model properties now I will consume the HTTP service developed using the ASP.NET Web API in a uncomplicated cshtml page with jQuery and Ajax.
For that in the View binder I will add together a folder named Car and inside that folder will add a CarStock named view. To add it but right-click on the View binder and select View.
The following snapshot shows how I had added the view.
After adding the view y'all volition get a blank view because we are not using any tightly coupled model here.
And then add a Controller with the name CarController. Call this view Carstock for the demo of consuming the Web API.
In this I called the view CarStock.
After adding the Controller and View at present let us motion dorsum towards the Spider web API and make some changes that we accept already created with the name "CarDetailsController".
Let's go to the first method in CarDetailsController.
- GET IEnumerable
- [HttpGet]
- public IEnumerable<CarsStock> GetAllcarDetails()
- {
- CarsStock ST =new CarsStock();
- CarsStock ST1 =new CarsStock();
- List<CarsStock> li =new List<CarsStock>();
- ST.CarName ="Maruti Waganor" ;
- ST.CarPrice ="four Lakh" ;
- ST.CarModel ="VXI" ;
- ST.CarColor ="Brown" ;
- ST1.CarName ="Maruti Swift" ;
- ST1.CarPrice ="five Lakh" ;
- ST1.CarModel ="VXI" ;
- ST1.CarColor ="RED" ;
- li.Add(ST);
- li.Add(ST1);
- render li;
- }
In this method, I take used the Model CarsStock and created a list of CarsStock "List<CarsStock>".
And returning it.
- GET by id
- public IEnumerable<CarsStock> Get( int id)
- {
- CarsStock ST =new CarsStock();
- CarsStock ST1 =new CarsStock();
- List<CarsStock> li =new List<CarsStock>();
- if (id == 1)
- {
- ST.CarName ="Maruti Waganor" ;
- ST.CarPrice ="4 Lakh" ;
- ST.CarModel ="VXI" ;
- ST.CarColor ="Brown" ;
- li.Add together(ST);
- }
- else
- {
- ST1.CarName ="Maruti Swift" ;
- ST1.CarPrice ="5 Lakh" ;
- ST1.CarModel ="VXI" ;
- ST1.CarColor ="Crimson" ;
- li.Add(ST1);
- }
- return li;
- }
- POST
- [HttpPost]
- public void PostCar([FromBody] CarsStock cs)
- {
- }
- PUT
- [HttpPut]
- public void Putcar( int id, [FromBody]CarsStock cs)
- {
- }
- DELETE
- [HttpDelete]
- public void Deletecar( int id)
- {
- }
In this DELETE method you lot can delete data (DELETE) from the database. I am using an id to delete the data.
Here is a snapshot of all the methods and models after adding the attributes to information technology.
Now let'south move to the view and do CRUD operations from at that place.
For getting a list of data I take created a function in jQuery.
- Calling GET IEnumerable List from Ajax and getting data from the Spider web API.
- <script lang= "ja" type= "text/javascript" >
- function AllcarDetails() {
- $.ajax({
- blazon:"GET" ,
- url:"http://localhost:32359/api/Cardetails" , //URI
- dataType:"json" ,
- success:part (data) {
- debugger ;
- var datadatavalue = data;
- var myJsonObject = datavalue;
- contentType:"application/json" ;
- $.each(myJsonObject,function (i, mobj) {
- $("#Cartbl" ).append( '<tr><td width="50px">' + mobj.CarName +
- '</td><td width="50px">' + mobj.CarModel +
- '</td><td width="50px">' + mobj.CarPrice +
- '</td>' + '</td><td width="50px">'
- + mobj.CarColor +'</td></tr>' );
- });
- },
- error:function (xhr) {
- alarm(xhr.responseText);
- }
- });
- }
- Calling PostCar Method using Ajax and posting data to the Spider web API.
- function PostData()
- {
- var cardetails =
- {
- CarName:"Ertiga" ,
- CarModel:"LXI" ,
- CarPrice:"5000000" ,
- CarColor:"blue"
- };
- $.ajax({
- type:"POST" ,
- data: JSON.stringify(cardetails),
- url:"http://localhost:32359/api/Cardetails" ,
- dataType:"json" ,
- contentType:"application/json" ,
- });
- }
- Calling the PUTcar method using Ajax and updating the data of the Web API.
- role PutData() {
- var cardetails =
- {
- CarName:"Ertiga" ,
- CarModel:"LXI" ,
- CarPrice:"5000000" ,
- CarColor:"blue"
- };
- var t = JSON.stringify(cardetails);
- var id ="0" ;
- $.ajax({
- url:'http://localhost:32359/api/Cardetails/' + id,
- blazon:"put" ,
- contentType:"application/json; charset=utf-viii" ,
- data: t,
- dataType:"json" ,
- });
- }
- Calling the Delete machine method using Ajax and to delete information of the Spider web API.
- function deleteData1()
- {
- var id = 0;
- $.ajax({
- url:'http://localhost:32359/api/CarDetails/' + id,
- type:'DELETE' ,
- success: role (data) {
- },
- error: function (data) {
- alert('Trouble in deleting automobile:' + information.responseText);
- }
- });
- }
- Calling GET by ID from Ajax and getting data from the Web API by id.
- function GetCarById() {
- var id = 1;
- $.ajax({
- url:'http://localhost:32359/api/CarDetails/' + id,
- type:'Go' ,
- dataType:"json" ,
- success: function (data) {
- var datavalue = information;
- var myJsonObject = datavalue;
- var CarModel = myJsonObject[0].CarModel;
- var CarName = myJsonObject[0].CarName;
- var CarColor = myJsonObject[0].CarColor;
- var CarPrice = myJsonObject[0].CarPrice;
- $('<tr><td>' + CarModel + '</td><td>' + CarName +
- '</td><td>' + CarColor + '</td>' + '</td><td>' + CarPrice + '</td></tr>' ).appendTo( '#Cartbl' );
- },
- error: function (xhr) {
- alert(xhr.responseText);
- }
- });
- }
Later completing all the functions of Ajax I am at present displaying the view "CarStock".
In the carstock view I have simply added 5 buttons and on the click event of every button a different function of the Web API is called.
The following snippet is debugged in Firebug.
Final Output
Here in this view I have consumed a Web API for Demo.
I know that at present all my web developer friends must have a basic understanding of what the Web API is.
Source: https://www.c-sharpcorner.com/UploadFile/4d9083/how-to-create-web-api-in-Asp-Net-mvc/
Posted by: cameronlacent.blogspot.com
0 Response to "How Do Web Services Integrate With Existing Api"
Post a Comment