Building rest-ful Services with ASP. WEBAPI (a)--simple example

Source: Internet
Author: User

Because the Web service that gives rest is very easy to use, it is becoming the preferred method for enterprise backend service integration. This article describes how to quickly build rest-ful services from Microsoft's ASP. NET Webapi.

Start by creating an ASP. NET Web application (I'm using Visual Studio 2013, which has built-in web API2).

In the Out template select Empty (empty project), and tick webapi. When you click OK, an empty WEBAPI service is created.

There is only one empty project at this time, there is no function, before the next step, we first look at the rest of the basic operating model, can be divided into the following four kinds:

    • post-Creating a resource
    • get-Retrieving Resources
    • put-Updating Resources
    • delete-Deleting Resources

A very classic crud model. The implementation of such a model in the Web API is straightforward, and a controller can be built directly using the wizard.

If you use a traditional wizard, remember to remove the 1 behind the wizard:

The default template content is as follows:

PublicClassValuescontroller:Apicontroller
{
GET api/<controller>
PublicIEnumerable<String> Get ()
{
ReturnNewString[] {"Value1","Value2"};
}

GET API/&LT;CONTROLLER&GT;/5
PublicStringGet (IntId
{
Return"Value";
}

POST api/<controller>
PublicvoidPost ([Frombody]StringValue
{
}

PUT API/&LT;CONTROLLER&GT;/5
Publicvoid Put (int ID, [frombody]string value"
        {
        }

        //DELETE api/<controller>/5< Span style= "color:black;" >
        public void Delete (int         {
         }
    }

This has actually helped us to achieve a basic service, but this service only implemented get, it supports the following two ways of the URL access (other ways can also access, but no specific effect):

    • api/values access to all value lists
    • Api/values/{id} access value by ID

Press CTRL + F5 to enter the appropriate address in the browser to see the results

The next thing we want to do is to improve it, to implement a simple query function, here I quote a Microsoft Official example:

PublicClassProductsController:Apicontroller
{
Product[] Products =NewProduct[]
{
NewProduct{Id = 1, Name ="Tomato Soup", Category ="Groceries", Price = 1},
NewProduct{Id = 2, Name ="Yo-Yo", Category ="Toys", Price = 3.75M},
NewProduct{Id = 3, Name ="Hammer", Category ="Hardware", Price = 16.99M}
};

PublicIEnumerable<Product> Get ()
{
ReturnProducts
}

PublicIhttpactionresultGet (IntId
{
VarProduct = products. FirstOrDefault (p) = P.id = = Id);
If(Product = =Null)
{
ReturnNotFound ();
}
ReturnOk (product);
}
}

PublicClassProduct
{
PublicIntId {Get;Set; }
Publicstring Name {get; set         public stringgetset         public decimal price {getset     }

At this point, we can see the results in the browser (because the controller changed the name, the address at this time becomes api/products)

So far, a simple rest Web service based on the ASP is built, and as space is limited, there is no more introduction, and more information can be found in Microsoft's official documentation: Getting Started with ASP. NET Web API 2. In addition, if you want to have a deeper understanding of rest, you can look at Infoq's article: Rest. As for the other content of the ASP, I'll probably write a few more articles to introduce it later.

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.