ASP. 2 The tenth lesson--using the Owin self-hosted web API

Source: Internet
Author: User
Tags hosting

Objective

Before reading this article, you can also go to the ASP. NET Web API 2 series navigation to view http://www.cnblogs.com/aehyok/p/3446289.html

This tutorial focuses on how to host Web APIs through Owin in a console application.

Open Web Interface for. NET (OWIN) creates an abstraction layer between the Web server and the Web application. Owin separates the Web application from the Web server and then leaves the application outside of IIS, which is hosted by the Owin program.

This article will still use VS2013. The sample code for this article http://pan.baidu.com/s/1msXF9

Using Owin self-hosted

1. Create a console application and then install Microsoft.AspNet.WebApi.OwinSelfHost through NuGet.

2. Configure the Web API to be self-hosted

To add a class named startup

Using Owin; Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Threading.Tasks; Using System.Web.Http; Namespace owinselfhost{public class Startup    {public void Configuration (Iappbuilder appBuilder)         {//Configure W EB API for Self-host.  httpconfiguration config = new httpconfiguration ();             Config. Routes.maphttproute (                 name: "Defaultapi",                 routetemplate: "Api/{controller}/{id}",                 defaults:new {id = Routeparameter.optional}             );             Appbuilder.usewebapi (config);         }     } }

It can be found primarily in order to configure routing.

3. Add a Web API controller

Using System; Using System.Collections.Generic; Using System.Linq; Using System.Text; Using System.Threading.Tasks; Using System.Web.Http; namespace Owinselfhost{class Valuescontroller:apicontroller    {//GET api/values public  ienumerable<string  > Get ()        {return new string[] {"value1", "value2"};        }//Get API/VALUES/5 public  string get (int id)        { return "value";        } Post api/values public  void post ([frombody]string value)        {        }//Put API/VALUES/5 public  void put (int ID, [frombody]string value]        {        }//delete API/VALUES/5 public  void Delete (int id)        {        }}     }

There's nothing special about the controller, it's simple.

4. Start Owin and use HttpClient to make a request to replace the contents of the file in the Program.cs with the following code

Using Microsoft.Owin.Hosting; Using System; Using System.Collections.Generic; Using System.Linq; Using System.Net.Http; Using System.Text; Using System.Threading.Tasks; Namespace Owinselfhost{class program    {static void Main (string[] args)        {string baseaddress = "http://localhost: 9000/"; Start OWIN host  using (webapp.start<startup> (url:baseaddress))            {//Create httpcient and make a request To api/values  HttpClient client = new HttpClient (); var response = client. Getasync (baseaddress + "Api/values"). Result;                Console.WriteLine (response);                Console.WriteLine (Response. Content.readasstringasync (). Result);            }            Console.ReadLine ();}}}    

Define a base address, primarily by Webapp.start to open Owin, to host the Web API, and then make the request through httpclient.

Final run, view effects

The returned result is correct.

Summarize

This feeling is still relatively simple, compared to the previous section of the http://www.cnblogs.com/aehyok/p/3456841.html more simple.

This article has been updated to the Web API Learning series Navigation http://www.cnblogs.com/aehyok/p/3446289.html

Reference links for this article Http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

The sample code for this article http://pan.baidu.com/s/1msXF9

ASP. 2 The tenth lesson--using the Owin self-hosted web API

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.