Create Database
Create a database with the name is LearnASPNETWebAPIWithRealApps. This database have a table: product table.
--
-- Table structure for table `product`
--
CREATE TABLE Product (
Id int IDENTITY(1,1) NOT NULL PRIMARY KEY,
Name varchar(50) NULL,
Price money NULL,
Quantity int NULL,
Status bit NULL,
CreationDate date NULL
)
--
-- Dumping data for table `product`
--
INSERT Product (Name, Price, Quantity, Status, CreationDate) VALUES ('Name 1', 12.3000, 3, true, '2017-10-20')
INSERT Product (Name, Price, Quantity, Status, CreationDate) VALUES ('Name 2', 25.1000, 8, false, '2017-11-12')
INSERT Product (Name, Price, Quantity, Status, CreationDate) VALUES ('Name 3', 23.9000, 2, true, '2017-12-26')
INSERT Product (Name, Price, Quantity, Status, CreationDate) VALUES ('Name 4', 89.2000, 11, false, '2017-09-17')
Structure of Product Table
Data of Product Table
ADO.NET Entity Data Model
Use the Entity Wizard to create an Entity Data Model From Database in Visual Studio.
Structure of Project
Create Web API Controller
Create Web API Controller use Entity Framework get application/json data from the client and use Entity Framework delete to database
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using Newtonsoft.Json;
using System.Net.Http.Headers;
using System.Data.Entity;
using LearnASPNETWebAPIWithRealApps.Models;
namespace LearnASPNETWebAPIWithRealApps.Controllers
{
[RoutePrefix("api/product")]
public class ProductRestController : ApiController
{
private LearnASPNETWebAPIWithRealAppsEntities db = new LearnASPNETWebAPIWithRealAppsEntities();
[HttpDelete]
[Route("delete/{id}")]
public HttpResponseMessage delete(int id)
{
try
{
var response = new HttpResponseMessage(HttpStatusCode.OK);
var product = db.Products.SingleOrDefault(p => p.Id == id);
db.Products.Remove(product);
db.SaveChanges();
return response;
}
catch
{
return new HttpResponseMessage(HttpStatusCode.BadRequest);
}
}
}
}
Consume Web API from Console Application
Create Console Application Project in Visual Studio
Create DemoRestClientModel
DemoRestClientModel class contain methods call Web API. Add reference to System.Net.Http.Formatting
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.Threading.Tasks;
using System.Net.Http;
using System.Net.Http.Headers;
namespace LearnASPNETWebAPIWithRealApps_Client
{
public class DemoRestClientModel
{
private string BASE_URL = "http://localhost:64967/api/product/";
public Task<HttpResponseMessage> delete(int id)
{
try
{
HttpClient client = new HttpClient();
client.BaseAddress = new Uri(BASE_URL);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
return client.DeleteAsync("delete/" + id);
}
catch
{
return null;
}
}
}
}
Run It
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
namespace LearnASPNETWebAPIWithRealApps_Client
{
class Program
{
static void Main(string[] args)
{
DemoRestClientModel demoRestClientModel = new DemoRestClientModel();
HttpResponseMessage httpResponseMessage = demoRestClientModel.delete(5).Result;
HttpStatusCode httpStatusCode = httpResponseMessage.StatusCode;
Console.WriteLine("Status Code: " + httpStatusCode);
bool isSuccessStatusCode = httpResponseMessage.IsSuccessStatusCode;
Console.WriteLine("IsSuccessStatusCode: " + isSuccessStatusCode);
Console.ReadLine();
}
}
}
Output
Status Code: OK
IsSuccessStatusCode: True