What is Web API
The ASP.NET 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.NET Web API is an ideal platform for building Restful applications on the .NET Framework. Referred from “Microsoft.com”.
The ASP.NET 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.NET Web API is an ideal platform for building Restful applications on the .NET Framework. Referred from “Microsoft.com”.
Why use the Web API
Currently, most mobile devices, browsers, and tablets are the medium for accessing most of the internet 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 Web API.
When to use it
If we want to expose the data/information of our application to our clients and other people then that other people can use our data and interact with the data/information we expose to them.
For example, a mobile application requires a service.
HTML 5 requires a service.
Desktop PC and tablets require services.
Currently most device apps require Web API services.
The ASP.Net Framework leverages both web standards such as HTTP, JSON, and XML and it provides a simple way to build and expose REST-based data services.
Some core concepts of ASP.Net MVC are similar to the ASP.Net Web API such as routing and controllers.
How to Implement
Step 1:
Start Visual Studio and select New Project from the Start page or from the File menu to select "File" -> "New" -> "Project".Step 1:
Step2:
Expand the Visual C# menu. Inside that Visual C# select Web. In the list of projects select ASP.Net MVC 4 Web Application. and fill the name EX: "WebAPIMVC"
Click -OK
->After adding, a new dialog will pop-up.
->Select Web API within Select a template
Step3:
->Right Click the Model(In right side)
->Add a Class(Ex: Employee.cs)
->Click Ok
and write the code within Employee.cs class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
namespace MvcWebAPI.Models
{
public class Employee
{
public int EmpId { get; set; }
public string EmpName { get; set; }
public string EmpAddress { get; set; }
public double EmpSalary { get; set; }
}
}
->Go to the Controller and open the controller and controller name ValuesController
->And the and the Namespace
using WebAPIMVC.Models;
-> and write the full code within the controller
using MvcWebAPI.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace MvcWebAPI.Controllers
{ public class ValuesController : ApiController
{ // GET api/values
public IEnumerable<Employee> Get()
{
return new List<Employee>()
{
new Employee(){
EmpId=1,
EmpName="mithilesh kumar",
EmpAddress="Hyderabad",
EmpSalary=6000
},
new Employee()
{
EmpId=2,
EmpName="sanjeev kumar",
EmpAddress="Patna",
EmpSalary=16000
}
};
}
// GET api/values/5
public Employee Get(int id)
{
if (id == 1)
{ return new Employee()
{ EmpId = 1,
EmpName = "mithilesh kumar",
EmpAddress = "Hyderabad",
EmpSalary = 6000
};
}
else
if (id == 2)
{
return new Employee()
{
EmpId = 2,
EmpName = "sanjeev kumar",
EmpAddress = "Patna",
EmpSalary = 16000
};
}
return null;
}
// POST api/values
public void Post([FromBody]string value)
{
} // PUT api/values/5
public void Put(int id, [FromBody]string value)
{
}
// DELETE api/values/5
public void Delete(int id)
{
}
}
}
Step4
-> Run the Problem
->And write the url
-Display the result in XML format on Google Chrome Browser
If we want to display the result in JSON ,Row, Plane Text ,WebView, TextView etc then we use Fiddler.
So how to use fiddler
->download the fiddler this link http://www.telerik.com/download/fiddler
->Select any radio button and click the download option.
->After install the fiddler
->After complete Open the Fiddler.
->And Run the mvc program and Copy the URL
->And Go to Composer option and click
->Click the dropdown list and select the get method
->And pest the mvc URL
->Click execute option(right side)
->Generate left side One URL
->Click the URL
->Display the result in JSON Format
->If we also display the result in a raw format the click raw option
->and view in notepad then click view in notepad option to the below right corner.
-> You can all option check...
So how to use fiddler
->download the fiddler this link http://www.telerik.com/download/fiddler
->Select any radio button and click the download option.
->After install the fiddler
->After complete Open the Fiddler.
->And Run the mvc program and Copy the URL
->And Go to Composer option and click
->Click the dropdown list and select the get method
->And pest the mvc URL
->Click execute option(right side)
->Generate left side One URL
->Click the URL
->Display the result in JSON Format
->If we also display the result in a raw format the click raw option
->and view in notepad then click view in notepad option to the below right corner.
-> You can all option check...
No comments:
Post a Comment