Asp.net Web API practice

Source: Internet
Author: User

Some functions, or APIs, need to be exposed for calls by other systems. Other systems may be Winform, WPF, Asp.net, or Mobile clients. Of course, it can be implemented using Web Service or WCF, but I prefer to try new things, so I plan to implement it using Asp.net Web API and try a RESTFull framework. For the first time, let's record the general process. The beauty name is "practice". It's actually an entry guide.

Asp.net Web API exposes the API to the client through the Http protocol. Many things are similar to ASp.net MVC. In fact, it is included in Asp.net Mvc 4. Next we will start the practice:

  1. Install Asp.net Mvc 4 at http://www.asp.net/mvc/mvc4. The installation takes about half an hour. Supports VS 2010 learning edition.
  2. Create a project, select the "Asp.net MVC 4 Web Application" project type, and then select Web API:

3. Focus on the ValuesController class under the Controller. This class exposes the following APIs:

public class ValuesController : ApiController    {        // GET api/values        public IEnumerable<string> Get()        {            return new string[] { "value1", "value2" };        }        // GET api/values/5        public string Get(int id)        {            return "value";        }        // POST api/values        public void Post(string value)        {        }        // PUT api/values/5        public void Put(int id, string value)        {        }        // DELETE api/values/5        public void Delete(int id)        {        }    }

If you are familiar with the http protocol, you can understand the above. F5 run, I use Chrome browser to test:

The returned result is in xml format, because Chrome automatically adds the "Accept: Application/Xml" Request Header (How do you make Chrome return Json ?). Json format is returned by default.

As for how URLs are routed, you should be very familiar with MVC. Take a look at App_Start/RouteConfig. cs. Of course, we can configure our own route here.

4. How does the client call the server-side API:

We usually use the HttpClient class. We can look at the HttpClient + ASP. NET Web API written by dudu boss, another option outside of WCF.

Here I will demonstrate how to use a simple WebClient for calling. The reason for using WebClient is: Unlike HttpClient, it does not need. net framework4.0.

WebClient wc = new WebClient (); wc. headers. add ("Accept: Application/Json"); // This line is not required, and the result is the same string json = wc. downloadString ("http: // localhost: 4391/api/values"); var ss = JavaScriptConvert. deserializeObject <string []> (json );

Here, Json parsing uses the third-party Newtonsoft. Json, which is a very old version. Download the latest version.

This third-party library is easier to use than Microsoft's own and powerful. It also supports. net and Sivlerlight. It is said that the speed is the fastest.

The Web Api automatically returns data in the corresponding format based on the client's Accept request header. The default format is JSON.

Here we only demonstrate Get. If WebClient is used for Post, I tried it and it failed. The value of the post method is always null. Google found that a better solution is to use RestSharp, which also supports. net/mono/silverlight/windowsphone. Have time to study.

5. The actual situation is over. :)

Address: http://www.cnblogs.com/slmk/archive/2012/08/09/2630691.html

 

Logistics, goods distribution, freight transportation, websites, forums, exchanges, information publishing

QQ website construction: 471226865

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.