ASP. net mvc Web API usage example

Source: Internet
Author: User

The rest api in asp.net mvc was mentioned in the rest service development in the previous blog. Due to space reasons, the rest api in asp.net mvc was not explained in the previous blog. I will discuss it here. The previous case is also referenced. The web api provided by asp.net mvc is used to develop the IP address home location query interface.

Because my machine is installed with the win8 Enterprise Edition operating system and the VS version is 2012, we choose to use the Web API in the MVC4 template that comes with VS to create a project.



After you click "OK", VS will automatically create a complete and runable ASP. NET Web API project for us.

The directory structure of the project shows that the structure of ASP. NET Web API is almost the same as that of ASP. net mvc. Delete the ValuesController file that we created and opened by default (for example files, refer ).

Since we want to create an IP address query service interface, we still use the GET request service in order to be consistent with the above service form, but this time we use the Web API in MVC to implement it.

First, create an Address model class in the Models folder.

namespace MvcWebApi.Models{    public class Address    {        public string IPAddress { get; set; }        public string Province { get; set; }        public string City { get; set; }      }}
Then we create an IPAddressController controller in the Controllers folder. Note that this IPAddressController must inherit from the ApiController class so that the service can be exposed.

Namespace MvcWebApi. controllers {public class IPAddressController: ApiController {private static IList addresses = new List {new Address () {IPAddress = "1.91.38.31", Province = "Beijing", City = "Beijing "}, new Address () {IPAddress = "210.75.225.254", Province = "Shanghai", City = "Shanghai" },}; public IEnumerable GetIPAddresses () {return addresses ;} public Address GetIPAddressByIP (string IP) {return addresses. firstOrDefault (x => x. IPAddress = IP );}}}
You only need to do the preceding two steps to run the project. Press Ctrl + F5 to run the entire project. The following page is displayed.


Click the API link in the upper-right corner.


You can see the usage and descriptions of the Web API interfaces we have defined.

Since it is a service and can be called by other programs, we need an environment that continuously guarantees its running. We can publish this well-written Web API project to IIS.

We can use the release function provided by VS to publish and map it to the IIS application directory.



Click Browse on the right side of IIS to see if the service is running properly.


As instructed in the document, enter http: // 192.168.0.2/webapi/api/ipaddress in the address bar.


We can see that we have received the data provided by the Service defined by the Web API. Let's try another interface method.


OK.

But what if we need to return the JSON format? There is a simple way to add a method in the Global. asax. cs file.


For the reason for this code, refer to: Explain.

After running this project, we will release it again.

When we run the statement in the browser again, we can see that the returned result is in JSON format by default (IE is in JSON format by default ).

ASP. NET Web API has been developed. For how to call it in the C # program, refer to the Code in my previous blog. If you want to call it on the page, you can request a URL through JS libraries such as JQuery.

References:

Http://www.asp.net/web-api/overview/getting-started-with-aspnet-web-api/tutorial-your-first-web-api

Http://blog.miniasp.com/post/2012/10/12/ASPNET-Web-API-Force-return-JSON-format-instead-of-XML-for-Google-Chrome-Firefox-Safari.aspx

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.