is what
Web API, Network application interface. It contains a wide range of functions, network applications through the API interface, can achieve storage services, messaging services, computing services, the ability to use these capabilities to develop a powerful Web application. In simple terms, it's an interface, for example, we're going to do the front and rear separation, the front end and the back end are connected through the URL, but how do we know if the backend data is up and the data returned is correct, so we know by this interface.
a similar technique
Postman and swagger.
Use steps
1. Create Web API project (this example uses vs2015)
FILE--New--Project--asp.net Web application
2. Select the Web API template in the template
[Routeprefix ("Api/ghost")]
Create a Web API project complete.
3. Code Writing
B, D How to write the previous how to write now, controller need to add some special code.
Step one: Remove the Using SYSTEM.WEB.MVC; Add a using System.Web.Http;
Step two: Give the Class A name, the route to find this class, so I want to give this class A name first:
[Routeprefix ("Api/ghost")]
Step three: Controller Inherits Apicontroller
Step four: Give the method a name, like a name for the class, route to find the class after the method:
[HttpGet]
[Route ("Goodsdetail")]
Complete code Reference:
Using System;
Using System.Collections.Generic;
Using System.Linq;
Using System.Web;
Using System.Web.Http;
Using Model;
Using ViewModel;
Using Services;
Namespace Webapi.controllers
{
[Routeprefix ("Api/ghost")] public
class Ghostcontroller:apicontroller
{
list<ghostvm> goods = new list<ghostvm> ();
Ghostservice goodservice = new Ghostservice ();
[HttpGet]
[Route ("Goodsdetail")]
Public list<ghostvm> Goodsdetail ()
{
String id = ' 1 ';
Goods = Goodservice.goodsdetail (ID);
if (goods = = null)
{return
null;
}
else
{return
goods;
}
}
}}
4. Test, at this time to install Webapitestclient.
Taskbar tool--nuget Package Manager--NuGet package for managing solutions--Browse Search webapitestclient--installation Webapi
5. Add the following code at the end of the areas/helppage/views/help/api.cshtml under Webapi:
@Html. Displayformodel ("Testclientdialogs")
@section scripts{
<link href= ' ~/areas/helppage/ Helppage.css ' rel= ' stylesheet '/>
@Html. Displayformodel ("Testclientreferences")
}
6. Run the project, click on the API on the Web page, click on the corresponding class name/method name, click the test API, you can see whether the connection database is correct.