Creation of the ASP. NET WEBAPI Service

Source: Internet
Author: User

Web API a RESTful architecture-style Web service. The so-called rest architecture has nothing to do with technology, but rather a resource-oriented software architecture design.

WCF has also provided support for restful styles since 3.5, but rather cumbersome compared to WEBAPI, WEBAPI provides a more lightweight communication architecture.

Let's see how to create a WEBAPI service

Start by creating a new solution, and create a new WEBAPI Project under the solution.

In the newly created WEBAPI project, the new controller (similar to the creation of MVC), we are named Customercontroller, in Customercontroller we provide the customer data

Simple Add and query functionality. The specific need to call warehousing to achieve internal logic processing, warehousing class code as follows

Model class
Namespace customerservice.models{public class  Customer    {        int id { get; set; }        public string name { get; SET;          public  string email { get;  SET; }    }
namespace customerservice.repository{ Public interface icustomerrepository { Customer getcust    Omer (int Add (customer customer);  }}
Namespace customerservice.repository{PublicClassCustomerrepository:icustomerrepository {PrivateStaticlist<Customer> customers =Newlist<Customer> () {NewCustomer () {ID = 1, Name ="Zhangsan", Email ="Zhangsan@dx.com"},NewCustomer () {ID = 2, Name ="Lisi", Email ="Lisi@dx.com"},NewCustomer () {ID = 3, Name ="Wangwu", Email ="Wangwu@dx.com"};PublicCustomer GetCustomer (int id) {Return customers.        FirstOrDefault (c = c.id = = ID); }Publicint Add (Customer customer) {int maxid = 0;if  (Customers != null)              {                 maxid = customers. Max (c => c.id);                 customer.ID = maxId;                 customers. ADD (Customer);            }             return maxId;         }    }}      

Here's a look at the controller's code.

namespacecustomerservice.controllers{ Public classCustomercontroller:apicontroller {Privateicustomerrepository customerrepository;  PublicCustomercontroller () {customerrepository=Newcustomerrepository (); }          PublicCustomer Getcustomerbyid (intID) {returnCustomerrepository.getcustomer (ID); }          Public intAddcustomer (Customer customer) {returnCustomerrepository.add (customer); }    }}

These are the steps to create a Web API service. There is also a routing configuration in Webapi that can personalize the address of the service. Specifically, the App_start file that is automatically generated in the project is added to the configuration in WebApiConfig.cs, the code is as follows

namespacecustomerservice{ Public Static classWebapiconfig { Public Static voidRegister (httpconfiguration config) {config.            Maphttpattributeroutes (); Config. Routes.maphttproute (Name:"GetCustomer", Routetemplate:"Api/{controller}/{id}", defaults:New{id =routeparameter.optional}); Config. Routes.maphttproute (Name:"Addcustomer", Routetemplate:"Api/{controller}/add", defaults:New{id =routeparameter.optional}); }    }}

The above code is called in the Application_Start () method (automatically generated when a new WEBAPI project is created)

namespace customerservice{    publicclass  WebApiApplication:System.Web.HttpApplication    {        protectedvoid  Application_Start ()        {            Globalconfiguration.configure (webapiconfig.register);     }}}

At this point, our first WEBAPI service has been created successfully, after starting running the project (service started state), enter the address of the service in the browser (chrom) to query a single customer

HTTP://LOCALHOST:24434/API/CUSTOMER/1 browser will show the results in XML format

ASP. NET Webapi Service creation

Related Article

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.