Create a simple Web application with asp.net web API + jquery

Source: Internet
Author: User
Tags http request

Read Dudu's "httpclient + asp.net Web API, another alternative to WCF," a demo that was created more than a long time ago to embody the ASP.net Web API. This is a Web application that involves simple crud operations only, and business logic is defined as a Web API and published as a service, and the foreground uses jquery to process user interaction and invoke the backend service.

De jure simple, crud-based Web applications

This simple demo application is used to simulate "contact management." When the page loads, all of the contact lists are listed. On the same page, we can add a new contact, or modify and delete existing contact information. The rendering effect of the entire application's unique page in the browser is shown in the following illustration.

Second, through the ASP.net Web API to provide services

Let's briefly describe the definition of a contact management service that is published as a Web API, and first look at the definition of the contact type used to represent contacts.

   1:public class Contact
2: {
3: Public string Id {get; set;}
4: Public string FirstName {get; set;}
5: Public string LastName {get; set;}
6: Public string Phoneno {get; set;}
7: Public string EmailAddress {get; set;}
8:}

The contact service is defined in the form of a contactcontroller with the following definition, which is a subclass of Apicontroller. For simplicity, we use static fields as storage for contact information. Contactcontroller defines get, put, post, and delete actions for obtaining, adding, modifying, and deleting contacts. I think people who don't know about the Web API have a sense of how to use the four commonly used HTTP methods as the names of operations, because they can be mapped to the HTTP request method by default.

1:public class Contactcontroller:apicontroller
2: {
3:private static list<contact> contacts = new list<contact>
4: {
5:new contact{id= "001", FirstName = "San", lastname= "Zhang", phoneno= "123", emailaddress= "zhangsan@gmail.com"},
6:new contact{id= "002", FirstName = "Si", lastname= "Li", phoneno= "456", emailaddress= "lisi@gmail.com"}
7:};
8:
9:public ienumerable<contact> Get ()
10: {
11:return contacts;
12:}
13:
14:public Contact get (string id)
15: {
16:return Contacts. FirstOrDefault (c => c.id = Id);
17:}
18:
19:public void put (contacts contact)
20: {
21:if (String. IsNullOrEmpty (Contact. ID))
22: {
23:contact. Id = Guid.NewGuid (). ToString ();
24:}
25:contacts. ADD (contact);
26:}
27:
28:public void Post (Contact contacts)
29: {
30:delete (Contact. ID);
31:contacts. ADD (contact);
32:}
33:
34:public void Delete (string id)
35: {
36:contact contact = Contacts. FirstOrDefault (c => c.id = Id);
37:contacts. Remove (contact);
38:}
39:}

As with the ASP.net MVC Web application, we also use URL routing to implement the mapping of the request address to the target Controller and action, while the path to the API default registration is shown below.

   1:public class MvcApplication:System.Web.HttpApplication
2: {
3: //...
4: Public static void RegisterRoutes (RouteCollection routes)
5: {
6: //...
7: routes. Maphttproute (
8: Name: "Defaultapi",
9: routetemplate: "Api/{controller}/{id}",
Ten: defaults:new {id = routeparameter.optional}
One: );
12:}

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.