博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Use OWIN to Self-Host ASP.NET Web API 2
阅读量:6614 次
发布时间:2019-06-24

本文共 3776 字,大约阅读时间需要 12 分钟。

 
 (OWIN)在Web服务器和Web应用程序之间建立一个抽象层。OWIN将网页应用程序从网页服务器分离出来,然后将应用程序托管于OWIN的程序而离开IIS之外。
 
 
Use OWIN to Self-Host ASP.NET Web API 2
 

This tutorial shows how to host ASP.NET Web API in a console application, using OWIN to self-host the Web API framework.

 (OWIN) defines an abstraction between .NET web servers and web applications. OWIN decouples the web application from the server, which makes OWIN ideal for self-hosting a web application in your own process, outside of IIS.

Software versions used in the tutorial

You can find the complete source code for this tutorial at .

Create a Console Application

On the File menu, click New, then click Project. From Installed Templates, under Visual C#, click Windows and then click Console Application. Name the project “OwinSelfhostSample” and click OK.

Add the Web API and OWIN Packages

From the Tools menu, click Library Package Manager, then click Package Manager Console. In the Package Manager Console window, enter the following command:

Install-Package Microsoft.AspNet.WebApi.OwinSelfHost

This will install the WebAPI OWIN selfhost package and all the required OWIN packages.

Configure Web API for Self-Host

In Solution Explorer, right click the project and select Add / Class to add a new class. Name the class Startup.

Replace all of the boilerplate code in this file with the following:

using Owin; using System.Web.Http; namespace OwinSelfhostSample { public class Startup { // This code configures Web API. The Startup class is specified as a type // parameter in the WebApp.Start method. 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); } } }

Add a Web API Controller

Next, add a Web API controller class. In Solution Explorer, right click the project and select Add / Class to add a new class. Name the classValuesController.

Replace all of the boilerplate code in this file with the following:

using System.Collections.Generic; using System.Web.Http; namespace OwinSelfhostSample { public class ValuesController : ApiController { // GET api/values public IEnumerable
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) { } } }

Start the OWIN Host and Make a Request Using HttpClient

Replace all of the boilerplate code in the Program.cs file with the following:

using Microsoft.Owin.Hosting; using System; using System.Net.Http; namespace OwinSelfhostSample { public class Program { static void Main() { string baseAddress = "http://localhost:9000/"; // Start OWIN host using (WebApp.Start
(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(); } } }

Running the Application

To run the application, press F5 in Visual Studio. The output should look like the following:

StatusCode: 200, ReasonPhrase: 'OK', Version: 1.1, Content: System.Net.Http.StreamContent, Headers: { Date: Tue, 09 Jul 2013 18:10:15 GMT Server: Microsoft-HTTPAPI/2.0 Content-Length: 19 Content-Type: application/json; charset=utf-8 } ["value1","value2"]

Additional Resources

This article was originally created on July 9, 2013

Author Information

Kanchan Mehrotra

Kanchan Mehrotra

Comments (23) 

 

转载地址:http://fseso.baihongyu.com/

你可能感兴趣的文章
Oracle 11g 间隔分区,导出报错 EXP-00006
查看>>
隐藏忽略的文件
查看>>
移动电商快速发展的原因分析
查看>>
Struts中常用的几种Action
查看>>
判断对象是否相等
查看>>
静态路由配置
查看>>
sqoop2 1.99.6 中遇到问题及源码修改汇总
查看>>
我的友情链接
查看>>
学习基于android+cordova的开发
查看>>
文本框鼠标悬停提示信息
查看>>
如何解决Weblogic的autodeploy不能实现自动部署
查看>>
菜鸟的日子
查看>>
用数据洞悉国内公有云发展:半数用户的选择
查看>>
centos定制
查看>>
CIO应该做些什么
查看>>
我的友情链接
查看>>
Train
查看>>
修改linux系统日志保存天数
查看>>
企业的七大类决策
查看>>
device eth0 does not seem to be present, delaying initialization
查看>>