ASP. NET web API tutorial (1) Your first web API

Source: Internet
Author: User

ASP. NET web API is a framework that can easily build HTTP-based services. It is widely used by clients. Including browsers and mobile devices. ASP. NET Web APIs is an ideal platform for building restful applications.ProgramNetwork framework.

Next, we will start to create the first web API.

 

Step 1: Obtain mvc4 support. The following link is the Visual Studio 2010 mvc4 patch.

Http://download.microsoft.com/download/2/F/6/2F63CCD8-9288-4CC8-B58C-81D109F8F5A3/AspNetMVC4Setup.exe

Step 2 after installation is complete, start Visual Studio 2010 to create a new project with the mvc4 Template

Click the mvc4 Project template to obtain the mvc4 project type.

Obtain the entire project of the web API

Web API projects have several features
First: the inherited controller is apicontroller.

Second: no corresponding View
Third, the route configuration is not in global. asax. CS, but is separated in a separate app_start folder.
Unified registration in global to facilitate single management. This is also a feature of mvc4.

Registration in global:

App_start directory

The most important webapiconfig

 

Public Static Void Register (httpconfiguration config) {config. routes. maphttproute (Name: " Defaapi API " , Routetemplate: " API/{controller}/{ID} " , Defaults: New {Id = routeparameter. optional });}

 

The access method is API/controller/parameter (ID)

Run now to get the effect

Access the default API/values to obtain the following results (captured using the F12 Network)

View detailed access results

Result Source

// Get API/Values
Public Ienumerable get ()
{
Return New String [] { " Value1 " , " Value2 " };
}

Try to return by ID: ModifyCodeIn order to intuitively see changes

 

// Get API/values/5
Public String Get ( Int ID)
{
Return " Value " + ID;
}

 

Access API/values/18

View the result

 

Source code in this example:
/Files/risk/web API 1/mvcapplication1.rar

 

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.