ASP. net web api: asp. netapi
This document describes how to create an ASP. NET Web Api program.
As the name suggests, a Web Api is an Api. The client can call this Api to perform business operations. Similar to ASP. NET web services (web service files with the extension. asmx,
ASP. NET Web APIs are mainly based on the ASP. net mvc framework.
Start now.
The development tool I used is opencl studio 2015.
1. Create an ASP. NET Web Api project.
Change the project name to DRMWebAPI. The following project structure is available.
So far, the API project has been created. Next, we will add a new Controller. Add the NewdealerController. cs file in the Controllers folder, for example:
After adding parameters, I want to use the post METHOD TO SEND Parameters to call this interface. Therefore, I added a class library DRMModel, for example.
The class library contains the main class NewDealer. The main class NewDealer contains five classes: Dealer, Contact, RDaddress, HouseAddress, and SendAddress.
The code for each class is as follows:
Dealer. cs
public class Dealer { public string RecruiterID { get; set; } public string Recruiter { get; set; } public string NationalID { get; set; } public string dealer_name { get; set; } public string sex { get; set; } public string BirthYear { get; set; } public string BirthMonth { get; set; } public string BirthDay { get; set; } }
Contact. cs
public class Contact { public string nightphonezonecode { get; set; } public string nightphone { get; set; } public string dayphonezonecode { get; set; } public string dayphone { get; set; } public string dayphoneext { get; set; } public string mobilephone { get; set; } public string email { get; set; } public string occupation { get; set; } public string occupation_sel { get; set; } public string RecommenderID { get; set; } public string RecommenderNationalID { get; set; } public string Recommender { get; set; } public string EINVOICE { get; set; } public string DONATEINVOICE { get; set; } }
RDaddress. cs
public class RDaddress { public string OLDMAILNO1 { get; set; } public string OLDADDRESS1 { get; set; } }
HouseAddress. cs
public class HouseAddress { public string selzipcode { get; set; } public string selprovince { get; set; } public string selcity { get; set; } public string seladdr { get; set; } public string sellane { get; set; } public string selalley { get; set; } public string selno { get; set; } public string selfl { get; set; } public string selroom { get; set; } public string selroomnum { get; set; } public string selrecipient { get; set; } }
SendAddress. cs
public class SendAddress { public string selzipcode1 { get; set; } public string selprovince1 { get; set; } public string selcity1 { get; set; } public string seladdr1 { get; set; } public string sellane1 { get; set; } public string selalley1 { get; set; } public string selno1 { get; set; } public string selfl1 { get; set; } public string selroom1 { get; set; } public string selroomnum1 { get; set; } public string selrecipient1 { get; set; } }
After the construction, add the following code to the NewdealerController. cs file:
using System.Web;using System.Web.Mvc;using System.Net.Http;using System.Web.Http;using System.Text;namespace DRMWebAPI.Controllers{ public class NewdealerController : ApiController { //post public string Add([FromBody]DRMModel.NewDealer newDealer) { return newDealer.Dealer.dealer_name; } }}
You can call the ASP. NET Web API to transmit json data. The API automatically converts json data to the C # type, provided that the json format is correct.
By analyzing the Add method in NewdealerController, we can see that when we transmit data in the DRMModel. NewDealer json format, the interface will return newDealer. Dealer. dealer_name.
Okay. How can I access this interface?
You can view the WebApiConfig. cs file under App_Start.
using System;using System.Collections.Generic;using System.Linq;using System.Web.Http;namespace DRMWebAPI{ public static class WebApiConfig { public static void Register(HttpConfiguration config) { config.Routes.MapHttpRoute( name: "DefaultApi", routeTemplate: "api/{controller}/{action}/{id}", defaults: new { id = RouteParameter.Optional } ); } }}
This file defines the API access path. As shown above, the path for accessing the Add method in NewdealerController is (Domain Name address/IP)/api/Newdealer/add.
Next, we try to call this interface. I add the following code to the HomeController. cs file:
Using System; using System. collections. generic; using System. linq; using System. web; using System. web. mvc; using System. net; using System. text; using System. IO; namespace DRMWebAPI. controllers {public class HomeController: Controller {public ActionResult Index () {string postdata = "{\" Dealer \ ":{\" RecruiterID \ ": \" 1 \", \ "Recruiter \": \ "2 \", \ "NationalID \": \ "2 \", \ "dealer_name \": \ "Ah, hahaha \", \ "sex \": \ "1 \", \ "BirthYear \": \ "1 \", \ "BirthMonth \": \ "1 \", \ "BirthDay \": \ "1 \"}, \ "Contact \": {\ "nightphonezonecode \": \ "1 \", \ "nightphone \": \ "1 \", \ "dayphonezonecode \": \ "1 \", \ "dayphone \": \ "1 \", \ "dayphoneext \": \ "1 \", \ "mobilephone \": \ "1 \", \ "email \": \ "1 \", \ "occupation \": \ "1 \", \ "occupation_sel \": \ "1 \", \ "RecommenderID \": \ "1 \", \ "RecommenderNationalID \": \ "1 \", \ "Recommender \": \ "1 \", \ "EINVOICE \": \ "1 \", \ "DONATEINVOICE \": \ "1 \"}, \ "RDaddress \": {\ "OLDMAILNO1 \": \ "1 \", \ "OLDADDRESS1 \": \ "1 \"}, \ "HouseAddress \": {\ "selzipcode \": \ "1 \", \ "selprovince \": \ "1 \", \ "selcity \": \ "1 \", \ "seladdr \": \ "1 \", \ "sellane \": \ "1 \", \ "selalley \": \ "1 \", \ "selno \": \ "1 \", \ "selfl \": \ "1 \", \ "selroom \": \ "1 \", \ "selroomnum \": \ "1 \", \ "selrecipient \": \ "1 \"}, \ "SendAddress \": {\ "selzipcode1 \": \ "1 \", \ "selprovince1 \": \ "1 \", \ "selcity1 \": \ "1 \", \ "seladdr1 \": \ "1 \", \ "sellane1 \": \ "1 \", \ "selalley1 \": \ "1 \", \ "selno1 \": \ "1 \", \ "selfl1 \": \ "1 \", \ "selroom1 \": \ "1 \", \ "selroomnum1 \": \ "1 \", \ "selrecipient1 \": \ "1 \"} "; string pageHtml =" "; var httpWebRequest = (HttpWebRequest) webRequest. create (" http://localhost:55706//api/Newdealer/add "); HttpWebRequest. contentType = "text/json"; httpWebRequest. method = "POST"; using (var streamWriter = new StreamWriter (httpWebRequest. getRequestStream () {streamWriter. write (postdata); streamWriter. flush (); streamWriter. close ();} var httpResponse = (HttpWebResponse) httpWebRequest. getResponse (); using (var streamReader = new StreamReader (httpResponse. getResponseStream () {pageHtml = streamReader. readToEnd ();} ViewData ["pram1"] = pageHtml; return View ();}}}
The Code simulates the browser's Post submission data and submits it to the http: // localhost: 55706 // api/Newdealer/add address.
Postdata is the json format data of the NewDealer class defined by myself. For how to convert the class and json data, Baidu.
Then, I add the Home folder in the View folder, and then add the Index. aspx file.
Add code:
<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<dynamic>" %><!DOCTYPE html>
Based on the above, when I open Home/Index, the page should output \ "dealer_name \": \ "Ah, hahaha \" in the json format of postdata, that is, the page will output: ah, hahaha
For example:
Demo: http://files.cnblogs.com/files/JinvidLiang/DRMWebAPI.zip