Technorati Tags: ASP. NET Web API
From the beginning of the work began to pay attention to the blog park, in the garden to learn a lot of knowledge, read a lot of cattle article, indeed never published a word. Recently changed the company, learning the Web API, to begin my first article, but also as a record of learning!
The introduction does not introduce much, tell me a little bit about my understanding of the ASP.
Unlike web Service, WCF, and ASP. NET Web APIs that directly access and process Http requests and responses, there is a lot less work to do in development, making it feel like everything is so smooth.
First, create an ASP. NET MVC 4 WEB Application
Select Web API
After a successful creation, an example is generated by default.
Public classvaluescontroller:apicontroller{//GET api/values Publicienumerable<string>Get () {return New string[] {"value1","value2" }; } //GET API/VALUES/5 Public stringGet (intID) {return "value"; } //POST api/values Public voidPost ([Frombody]stringvalue) { } //PUT API/VALUES/5 Public voidPut (intID, [frombody]stringvalue) { } //DELETE API/VALUES/5 Public voidDelete (intID) {}}
I'm stealing a lazy here, directly using the Northwind database, downloading a Northwind database from the Web, restoring to a local SQL Server, and then creating a new ADO. NET Entity Data Model
Select Generate from Database,next
Click New Connection
Select the local database, enter the user name password, select NORTHWIND, test the connection is successful, OK
Select Yes (will generate an app. config file, after successful, to copy the inside NorthwindEntities connectionStrings to the WEB project Webconfig), click Next
Select Tables,finish
Add a new Class in models, Employee
Copy the attributes from the employee class in the edmx file
Public classemployee{ Public intEmployeeID {Get;Set; } Public stringLastName {Get;Set; } Public stringFirstName {Get;Set; } Public stringTitle {Get;Set; } Public stringTitleOfCourtesy {Get;Set; } PublicNullable<system.datetime> BirthDate {Get;Set; } PublicNullable<system.datetime> HireDate {Get;Set; } Public stringAddress {Get;Set; } Public stringCity {Get;Set; } Public stringRegion {Get;Set; } Public stringPostalCode {Get;Set; } Public stringCountry {Get;Set; } Public stringHomePhone {Get;Set; } Public stringExtension {Get;Set; } Public byte[] Photo {Get;Set; } Public stringNotes {Get;Set; } Publicnullable<int> ReportsTo {Get;Set; } Public stringPhotopath {Get;Set; }}
I used the automapper to convert the DTO into Model, and if not, you can use NuGet to download a
Usage Reference: Https://github.com/AutoMapper/AutoMapper/wiki
After opening the Global.asax.cs, add the Mapper method, so that in the Controller, you can directly convert.
protected void Application_Start ()
{ arearegistration.registerallareas (); Webapiconfig.register (globalconfiguration.configuration); Filterconfig.registerglobalfilters (globalfilters.filters); Routeconfig.registerroutes (routetable.routes); Bundleconfig.registerbundles (bundletable.bundles); Mapper ();} Private void Mapper () { //model to DTO automapper.mapper.createmap<dx. MVCWebAPI.Models.Employee, DX. Dataaccess.employee>(); // DTO to model Automapper.mapper.createmap<dx. Dataaccess.employee, DX. Mvcwebapi.models.employee>();}
Once saved, create a controller,employeescontroller.
Add the following method, AutoMapper can directly replace the collection of DTOs
Public Ienumerable<models.employee> getallemployees ()
{
var New List<models.employee>(); using (varnew northwndentities ()) {
= Automapper.mapper.map<system.data.entity.dbset<dataaccess.employee>, List<Models.Employee>> (context. Employees); }
return list. ToArray ();
}
Finally add a new view in the Views/home, Employee, remember to add action in HomeController
Employee View page Code
<DivID= "Body"> <inputtype= "button"ID= "GetAll"value= "GetAll" /> <DivID= "Employeeall"> </Div> <Scriptsrc= "~/scripts/jquery-1.8.2.min.js"></Script> <Scripttype= "Text/ecmascript">$ (). Ready (function () { $("#getAll"). Click (function() {$.getjson ("/api/v1/employees"). Done (function(data) {varHTML= "<ul>"; $ (data). each (function(I, item) {HTML+= "<li>" +item. EmployeeID+ " | " +item. LastName+ " " +item. FirstName+ " | " +item. Title+ "</li>"; }); HTML+= "</ul>"; $("#employeeAll"). HTML (HTML); }); }); }); </Script></Div>
Execute the program, click GetAll, and you are done.
There is a wrong place, and you are welcome to point out.
As a bitter procedural ape, I would like to record the growth on the road of the little drops.
The ASP. NET Web API learns a