Web Api Using AngularJS
using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using AngularJSSample.Models;
namespace AngularJSSample.Controllers
{
[RoutePrefix("api/employee")]
public class EmployeesController : ApiController
{
private EmployeeEntities db = new EmployeeEntities();
// GET: api/Employees
[HttpGet]
[AllowAnonymous]
[Route("getemployee")]
public IQueryable<Employee> GetEmployees()
{
return db.Employees;
}
// GET: api/Employees/5
//[ResponseType(typeof(Employee))]
[HttpGet]
[AllowAnonymous]
[Route("employee")]
public IHttpActionResult GetEmployee(int id)
{
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
return Ok(employee);
}
// PUT: api/Employees/5
[ResponseType(typeof(void))]
public IHttpActionResult PutEmployee(int id, Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != employee.eno)
{
return BadRequest();
}
db.Entry(employee).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Employees
// [ResponseType(typeof(Employee))]
[HttpPost]
[AllowAnonymous]
[Route("PostEmployee")]
public IHttpActionResult PostEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Employees.Add(employee);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (EmployeeExists(employee.eno))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = employee.eno }, employee);
}
// DELETE: api/Employees/5
[ResponseType(typeof(Employee))]
public IHttpActionResult DeleteEmployee(int id)
{
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
db.Employees.Remove(employee);
db.SaveChanges();
return Ok(employee);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EmployeeExists(int id)
{
return db.Employees.Count(e => e.eno == id) > 0;
}
}
}
For HTML page
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="Scripts/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<table border="1">
<tr ng-repeat="dataa in remidy">
<td>{{dataa.eno}}</td>
<td>{{dataa.ename}}</td>
<td>{{dataa.salary}}</td>
</tr>
</table>
<button ng-click="myfuntion()">Click Me</button>
<table border="1">
<tr ng-repeat="dataa in rem">
<td>{{dataa.eno}}</td>
<td>{{dataa.ename}}</td>
<td>{{dataa.salary}}</td>
</tr>
</table>
Employee Id <input type="number" name="txtEno" id="txtEno" ng-model="txtEno" />
Employee Name<input type="text" id="txtEname" name="txtEname" ng-model="txtEname" />
Employee Salary<input type="number" id="txtSalary" name="txtSalary" ng-model="txtSalary" />
<button ng-click="myfun(txtEno,txtEname,txtSalary)">Save</button>
<!--app.controller('myController', function ($scope, $http) {
$http.get("http://localhost:49791/api/employee/GetEmployee").then(function (response) {
$scope.remidy = response.data;
});-->
<!--<div ng-repeat="dataa in remidy"> </div>-->
</div>
</body>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function ($scope, $http) {
$scope.myfun = function (txtEnos, txtEnames, txtSalarys) {
debugger;
$http({
method: 'POST',
url: 'http://localhost:49791/api/employee/PostEmployee',
data: '{"EmployeeDetails":[],"eno":"' + txtEnos + '","ename":"' + txtEnames + '","salary":"' + txtSalarys + '"}',
headers: { 'Content-Type': 'application/json' }
}).then(function mySucess(response) {
alert("sucess");
}, function myFailure(responce) {
alert("Failed");
});
}
var count = 0;
$scope.myfuntion = function () {
if (count % 2 == 0) {
$http({
method: "GET",
url: "http://localhost:49791/api/employee/GetEmployee"
}).then(function mySucess(respone) {
$scope.rem = respone.data;
}, function myFailure(responce) {
responce.ExceptionMessage;
});
count++;
} else {
$scope.rem = [];
count++;
}
}
$http({
method: "GET",
url: "http://localhost:49791/api/employee/GetEmployee"
}).then(function mySucess(respone) {
$scope.remidy = respone.data;
}, function myFailure(responce) {
responce.ExceptionMessage;
});
});
</script>
</html>
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Data.Entity.Infrastructure;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using System.Web.Http.Description;
using AngularJSSample.Models;
namespace AngularJSSample.Controllers
{
[RoutePrefix("api/employee")]
public class EmployeesController : ApiController
{
private EmployeeEntities db = new EmployeeEntities();
// GET: api/Employees
[HttpGet]
[AllowAnonymous]
[Route("getemployee")]
public IQueryable<Employee> GetEmployees()
{
return db.Employees;
}
// GET: api/Employees/5
//[ResponseType(typeof(Employee))]
[HttpGet]
[AllowAnonymous]
[Route("employee")]
public IHttpActionResult GetEmployee(int id)
{
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
return Ok(employee);
}
// PUT: api/Employees/5
[ResponseType(typeof(void))]
public IHttpActionResult PutEmployee(int id, Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
if (id != employee.eno)
{
return BadRequest();
}
db.Entry(employee).State = EntityState.Modified;
try
{
db.SaveChanges();
}
catch (DbUpdateConcurrencyException)
{
if (!EmployeeExists(id))
{
return NotFound();
}
else
{
throw;
}
}
return StatusCode(HttpStatusCode.NoContent);
}
// POST: api/Employees
// [ResponseType(typeof(Employee))]
[HttpPost]
[AllowAnonymous]
[Route("PostEmployee")]
public IHttpActionResult PostEmployee(Employee employee)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
db.Employees.Add(employee);
try
{
db.SaveChanges();
}
catch (DbUpdateException)
{
if (EmployeeExists(employee.eno))
{
return Conflict();
}
else
{
throw;
}
}
return CreatedAtRoute("DefaultApi", new { id = employee.eno }, employee);
}
// DELETE: api/Employees/5
[ResponseType(typeof(Employee))]
public IHttpActionResult DeleteEmployee(int id)
{
Employee employee = db.Employees.Find(id);
if (employee == null)
{
return NotFound();
}
db.Employees.Remove(employee);
db.SaveChanges();
return Ok(employee);
}
protected override void Dispose(bool disposing)
{
if (disposing)
{
db.Dispose();
}
base.Dispose(disposing);
}
private bool EmployeeExists(int id)
{
return db.Employees.Count(e => e.eno == id) > 0;
}
}
}
For HTML page
<!DOCTYPE html>
<html>
<head>
<title></title>
<meta charset="utf-8" />
<script src="Scripts/angular.min.js"></script>
</head>
<body>
<div ng-app="myApp" ng-controller="myController">
<table border="1">
<tr ng-repeat="dataa in remidy">
<td>{{dataa.eno}}</td>
<td>{{dataa.ename}}</td>
<td>{{dataa.salary}}</td>
</tr>
</table>
<button ng-click="myfuntion()">Click Me</button>
<table border="1">
<tr ng-repeat="dataa in rem">
<td>{{dataa.eno}}</td>
<td>{{dataa.ename}}</td>
<td>{{dataa.salary}}</td>
</tr>
</table>
Employee Id <input type="number" name="txtEno" id="txtEno" ng-model="txtEno" />
Employee Name<input type="text" id="txtEname" name="txtEname" ng-model="txtEname" />
Employee Salary<input type="number" id="txtSalary" name="txtSalary" ng-model="txtSalary" />
<button ng-click="myfun(txtEno,txtEname,txtSalary)">Save</button>
<!--app.controller('myController', function ($scope, $http) {
$http.get("http://localhost:49791/api/employee/GetEmployee").then(function (response) {
$scope.remidy = response.data;
});-->
<!--<div ng-repeat="dataa in remidy"> </div>-->
</div>
</body>
<script>
var app = angular.module('myApp', []);
app.controller('myController', function ($scope, $http) {
$scope.myfun = function (txtEnos, txtEnames, txtSalarys) {
debugger;
$http({
method: 'POST',
url: 'http://localhost:49791/api/employee/PostEmployee',
data: '{"EmployeeDetails":[],"eno":"' + txtEnos + '","ename":"' + txtEnames + '","salary":"' + txtSalarys + '"}',
headers: { 'Content-Type': 'application/json' }
}).then(function mySucess(response) {
alert("sucess");
}, function myFailure(responce) {
alert("Failed");
});
}
var count = 0;
$scope.myfuntion = function () {
if (count % 2 == 0) {
$http({
method: "GET",
url: "http://localhost:49791/api/employee/GetEmployee"
}).then(function mySucess(respone) {
$scope.rem = respone.data;
}, function myFailure(responce) {
responce.ExceptionMessage;
});
count++;
} else {
$scope.rem = [];
count++;
}
}
$http({
method: "GET",
url: "http://localhost:49791/api/employee/GetEmployee"
}).then(function mySucess(respone) {
$scope.remidy = respone.data;
}, function myFailure(responce) {
responce.ExceptionMessage;
});
});
</script>
</html>
Comments
Post a Comment