In This Blog, You Can Have The Complete Source Code Of Creating And Consuming ASP.NET Web API using Visual Studio.
HomeController Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
namespace AspNetFirstWebApi.Controllers
{
public class HomeController : Controller
{
// GET: Home
public ActionResult Index()
{
return View();
}
}
}
Index.cshtml Source Code
@{
ViewBag.Title = "Index";
}
<script src="~/scripts/jquery.js"></script>
<h2>Index</h2>
<input type="button" value="Get All Employees" id="btn1" />
<br />
<input type="text" placeholder="Enter Employee ID" id="txt" />
<input type="button" value="Get Employee By Id" id="btn3" />
<input type="button" value="Clear" id="btn2" />
<br />
<div id="ShowData">
</div>
<script>
$(document).ready(function () {
$("#btn3").click(function () {
var id = $("#txt").val();
$.getJSON('/api/EmployeesData/' + id, function (responseData) {
var item = "<h2>" + responseData + "</h2>";
$(item).appendTo("#ShowData");
});
});
$("#btn2").click(function () {
$("#ShowData").empty();
});
$("#btn1").click(function () {
$.getJSON('/api/EmployeesData', function (responseData) {
$.each(responseData, function (key, val) {
var item = "<h2>" + val + "</h2>";
$(item).appendTo("#ShowData");
});
});
});
});
</script>
WebApiConfig.cs File Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
namespace AspNetFirstWebApi
{
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API configuration and services
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{id}",
defaults: new { id = RouteParameter.Optional }
);
}
}
}
EmployeesDataController.cs Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
namespace AspNetFirstWebApi.Controllers
{
public class EmployeesDataController : ApiController
{
public string[] myEmployees = { "Adil","Bilal","Zain" };
[HttpGet]
public string[] GetEmployees()
{
return myEmployees;
}
[HttpGet]
public string GetEmployeeByIndex(int id)
{
return myEmployees[id];
}
}
}
Global.asax.cs Source Code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Routing;
using System.Web.Security;
using System.Web.SessionState;
using System.Web.Http;
namespace AspNetFirstWebApi
{
public class Global : HttpApplication
{
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
AreaRegistration.RegisterAllAreas();
GlobalConfiguration.Configure(WebApiConfig.Register);
RouteConfig.RegisterRoutes(RouteTable.Routes);
}
}
}
_Layout.cshtml Source Code
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>@ViewBag.Title - My ASP.NET Application</title>
<link href="~/Content/Site.css" rel="stylesheet" type="text/css" />
<link href="~/Content/bootstrap.min.css" rel="stylesheet" type="text/css" />
<script src="~/Scripts/modernizr-2.6.2.js"></script>
</head>
<body>
<div class="navbar navbar-inverse navbar-fixed-top">
<div class="container">
<div class="navbar-header">
<button type="button" class="navbar-toggle" data-toggle="collapse" data-target=".navbar-collapse">
<span class="icon-bar"></span>
<span class="icon-bar"></span>
<span class="icon-bar"></span>
</button>
@Html.ActionLink("Application name", "Index", "Home", new { area = "" }, new { @class = "navbar-brand" })
</div>
<div class="navbar-collapse collapse">
<ul class="nav navbar-nav">
</ul>
</div>
</div>
</div>
<div class="container body-content">
@RenderBody()
<hr />
<footer>
<p>© @DateTime.Now.Year - My ASP.NET Application</p>
</footer>
</div>
<script src="~/Scripts/jquery-1.10.2.min.js"></script>
<script src="~/Scripts/bootstrap.min.js"></script>
</body>
</html>
Click Below Link to Download Source Code Of This Blog
https://www.mediafire.com/file/bkajkp2mj9ft9c7/AspNetFirstWebApi.rar/file
No responses yet