Asp.Net Web API 2第十課——使用OWIN自承載Web API

來源:互聯網
上載者:User

標籤:

前言

閱讀本文之前,您也可以到Asp.Net Web API 2 系列導航進行查看 http://www.cnblogs.com/aehyok/p/3446289.html

本教程主要來展示在控制台應用程式中如何通過OWIN來承載Web API。

Open Web Interface for .NET (OWIN)在Web伺服器和Web應用程式之間建立一個抽象層。OWIN將網頁應用程式從網頁伺服器分離出來,然後將應用程式託管於OWIN的程式而離開IIS之外。

本文仍將使用VS2013。 本文的範例程式碼http://pan.baidu.com/s/1msXF9

使用OWIN自承載

 1.建立一個控制台應用程式,然後通過Nuget來安裝Microsoft.AspNet.WebApi.OwinSelfHost。

2.配置Web API為自承載

添加一個名為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 Web 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);         }     } }

可以發現主要是為了配置路由。

 3.添加一個Web API控制器

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)        {        }     }}

控制器也沒什麼特別的地方,很簡單。

4.啟動OWIN,並且使用HttpClient發出請求,使用以下代碼替換調Program.cs中檔案的內容

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();         }    }}

定義一個基地址,主要是通過WebApp.Start來開啟OWIN,來承載Web API,然後就可以通過HttpClient進行調用發出請求。

最終運行,查看效果

返回的結果是正確的。

總結

 這感覺上去還是比較簡單的,相對於上一節中http://www.cnblogs.com/aehyok/p/3456841.html更為簡單吧。

 本文已更新至Web API學習系列導航 http://www.cnblogs.com/aehyok/p/3446289.html

 本文的參考連結http://www.asp.net/web-api/overview/hosting-aspnet-web-api/use-owin-to-self-host-web-api

 本文的範例程式碼http://pan.baidu.com/s/1msXF9

Asp.Net Web API 2第十課——使用OWIN自承載Web API

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.