Web Api Smple With DTO API MVC
/*DTO*/
public class EmpDTO
{
public int EmpNo { get; set; }
public string EmpName { get; set; }
public int Salary { get; set; }
public string DeptName { get; set; }
public string Designation { get; set; }
}
/*API*/
public class EmpCls:IEmpCls
{
public EmpDTO GetEmployee(string EmpName)
{
using (GeesemedLocalEntities DB = new GeesemedLocalEntities())
{
var Module = from p in DB.GetEmpDetails("")
where p.EmpName.Contains(EmpName)
select new EmpDTO
{
EmpName=p.EmpName,
Salary=p.Salary,
DeptName=p.DeptName,
Designation=p.Designation
};
return Module.SingleOrDefault();
}
}
}
interface IEmpCls
{
EmpDTO GetEmployee(string EmpName);
}
/*API Controller*/
[RoutePrefix("api/EmpApi")]
public class EmpApiController : ApiController
{
static readonly IEmpCls EmpRep = new EmpCls();
[Route("GetEmpDetails")]
public HttpResponseMessage GetEmpDetails(string EmpName)
{
EmpDTO objEmpDTO = EmpRep.GetEmployee(EmpName);
if (objEmpDTO == null)
{
return Request.CreateErrorResponse(HttpStatusCode.NotFound, "Sorry Not Call This Method");
}
else
{
return Request.CreateResponse<EmpDTO>(objEmpDTO);
}
}
/*MVC Controller*/
public ActionResult GetEmployee()
{
return View();
}
public ActionResult GetEmployee(EmpDTO objDTO)
{
var client = new HttpClient();
if (objDTO.EmpName != null)
{
var response = client.PostAsJsonAsync("http://localhost:6198/api/EmpApi/GetEmpDetails", objDTO).Result;
if (response.IsSuccessStatusCode)
{
}
}
return View();
}
/*View*/
@model SampleMapper.EmpDTO
@{
ViewBag.Title = "GetEmployee";
}
<h2>GetEmployee</h2>
@using (Html.BeginForm("GetEmployee", "Emp", FormMethod.Post, new { id = "frmEmp" }))
{
@Html.ValidationSummary(true)
@Html.TextBoxFor(model => model.EmpName, new { tabindex = 1, maxlength = 10, @class = "form-control", placeholder = "EmpName", id = "EmpName" })
<br />
<input type="submit" value="Submit" class="btn btn-def btn-block button" formaction="/Emp/GetEmployee" />
}
}