Web API Use Tutorial __ Project Combat

Source: Internet
Author: User
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.


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.