ASP.NET Web API is a framework based on ASP.NET for building HTTP endpoints. The ASP.NET Web API can be built as a part of ASP.NET Webforms or MVC to function as the services layer of a web, mobile or client desktop application.
ASP.NET Web API is recommended over WCF in many situations except if you are limited to use .NET 3.5 and/or if you need to expose a SOAP based service endpoints.
ASP.NET Web API is different from REST since it may not fully comply with a RESTful architecture, however, a REST based service can be built on top of Web API.
Creating a simple ASP.NET Web API
1. Open Visual studio2. Create a new project Template > Visual C# > Web > ASP.NET Empty Web Application
3. Right click on the project > Manage NuGet Packages...
> Search for Web API > select Microsoft ASP.NET Web API > click Install
> Click Close
4. Add a new class to the project, name it MyApiController.
5. Derive the new class from ApiController class and add a public method called Get() that returns string value.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
namespace WebAPIEx1
{
public class MyApiController : ApiController
{
public string Get()
{
return "Hello
World";
}
}
}
7. Right click the project > select Add > New Item... > select Global Application Class > click Add.
8. In the application_start method, add the following line:
GlobalConfiguration.Configuration.Routes.Add("default", new HttpRoute("{controller}"));
9. Build the application
10. Right click on the project and click view in browser.
12. Open the .json file in a note pad.
Reference
WCF and ASP.NET Web API - https://msdn.microsoft.com/en-us/library/jj823172(v=vs.110).aspx
Comments
Post a Comment