introduction of Owin
OWIN's full name is "Open Web Interface for. Net", OWIN defines a standard set of interfaces between. NET Web servers and. NET Web applications, designed to decouple the server from the application, making the portable. NET Web Applications and cross-platform aspirations are a reality, and standard OWIN apps can run on any OWIN compatible server, no longer dependent on Windows and IIS.
Use of Owin
- Add to Project Microsoft.AspNet.WebApi.Owin and Microsoft.AspNet.WebApi.Owin Self host package (self host to open Owin host, set listener to accept HTTP requests)
Startup is the Owin convention, used to configure the Owin, the code is as follows:
Using owin;using system;using system.collections.generic;using system.linq;using system.text;using system.threading.tasks;using system.web.http;namespace owintest{Public class Startup {public void Config (iappbuilder appBuilder) { //create Web API configuration var config = new httpconfiguration (); Enable the tag Route config. Maphttpattributeroutes (); The default Web API Route config. Routes.maphttproute ( name: "Defaultapi", routetemplate: "Api/{controller}/{id}", defaults:new {id = Routeparameter.optional} ); Attach the routing configuration to appBuilder appbuilder.usewebapi (config);}}
- Create a new Controllers folder, add the Querycontrollers class
Follow the conventions of the Web API project, add a folder named Controllers to your project, and then create a new Querycontroller class that sets its base class to System.Web.Http.ApiController as As an example, the content is consistent with the Web API Controller template that comes with Visual Studio, and contains 4 request methods (Get/post/put/delete) for demonstration, Override the Get method (return request parameters directly) and the Post method (Accept entity class parameters directly return), the Querycontroller code is as follows:
Using system;using system.collections.generic;using system.linq;using system.text;using System.Threading.Tasks; Using System.web.http;namespace owintest.controllers{public class Querycontroller:apicontroller { // Get API Public string get (string id) { return ID; } Post API Public Deparams post (deparams dp) { return dp; } Put API public void put (int id, string value) { } //delete API public void Delete (int id)
{ } }}
- Add the Owin startup method to the Main method in the Program.cs file, with the following code:
Using system;using system.collections.generic;using system.linq;using system.net.http;using System.Text;using System.threading.tasks;using microsoft.owin.hosting;namespace owintest{ class program { static void Main (string[] args) { string baseaddress = "Http://localhost:9000/"; Start OWIN host using (webapp.start<startup> (url:baseaddress)) { HttpClient client = new HttpClient (); Console.WriteLine (baseaddress); Console.ReadLine ();}}}
Demo
- Start the service-side program
DHC is an online tool for testing the Http/rest API, https://www.sprintapi.com/dhcs.html, and local testing is required to download extensions in the Google Store.
It is also possible to send a GET request directly via the browser, as follows:
Where Localhost:9000/api/query/ok corresponds to routetemplate in the Owin routing configuration: "Api/{controller}/{id}".
Specify the request content format as JSON
The above is the routing rule for API requests (Routetemplate: "Api/{controller}/{id}"), below is a brief description of the routing rule (routetemplate: "{controller}/{action) requested on the common MVC page. }/{id} "), customize the method of request mode.
-First modify the routing rule in startup, add {action} after {controller}, then the {ID} will correspond to the parameters in the action. The modified startup is as follows:
-Next, add a custom method to the previous Querycontroller class, declaring the request method ([HttpGet], [HttpPost], [Httpdelete], [httpput], and so on), and then add the custom method, as follows:
-Demo
Some places are not detailed enough, I will add later, understand the wrong place, but also ask you to criticize.
C # uses Owin to create the Web API