Owin 自寄宿 asp.net web api
2016-12-18 09:13
134 查看
http://owin.org/
Owin 定义了webserver和webapplication之间的标准接口,目标就是为了解耦webapplication对webserver的依赖,
就是说以后可以轻松的建立一个轻量级的HttpServer,
Step1:
Step2:Configuration in Startup.css
app.UseErrorPage();
为WebApplication指定错误页,
app.Run加入响应逻辑
Step3:指定监听端口
OK,一切准备就绪,启动这个Console程序,请求 http://localhost:9000/ 这个地址,看起来比NodeJS更加方便。
2.Self Host Web API using Owin
Step1:
Step2:Startup.cs
Step3:APIController
Step4:设置监听端口
Step5:启动Console程序,这里我们看到返回的是Json格式的数据,当我们用火狐浏览器进行请求,会发现返回的是XML格式的数据,因为火狐浏览器默认的Accept为XML
Owin 定义了webserver和webapplication之间的标准接口,目标就是为了解耦webapplication对webserver的依赖,
就是说以后可以轻松的建立一个轻量级的HttpServer,
1.Basic Sample Self Host
下面建立一个Basic Self Host Http Server Via Owin ,全部功能就是获取客户端的Http请求,然后做出回应,当发生Unhandled Exception的时候,会自动跳转到错误页,Step1:
Install-Package Microsoft.Owin.SelfHost
Step2:Configuration in Startup.css
using System; using System.Threading.Tasks; using Microsoft.Owin; using Owin; namespace OwinSelfHostBasic { public class Startup { public void Configuration(IAppBuilder app) { app.UseErrorPage(); // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=316888 app.Run(context => { if (context.Request.Path.Value == "/fail") { throw new Exception("Random exception"); } context.Response.ContentType = "text/plain"; return context.Response.WriteAsync("Hello, world."); }); } } }
app.UseErrorPage();
为WebApplication指定错误页,
app.Run加入响应逻辑
Step3:指定监听端口
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; namespace OwinSelfHostBasic { class Program { static void Main(string[] args) { using (Microsoft.Owin.Hosting.WebApp.Start<Startup>("http://localhost:9000")) { Console.WriteLine("Press [enter] to quit..."); Console.ReadLine(); } } } }
OK,一切准备就绪,启动这个Console程序,请求 http://localhost:9000/ 这个地址,看起来比NodeJS更加方便。
2.Self Host Web API using Owin
Step1:
Install-Package Microsoft.AspNet.WebApi.OwinSelfHost
Step2:Startup.cs
using Owin; using System.Web.Http; namespace OwinWebAPISelfHost { 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); } } }
Step3:APIController
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Web.Http; namespace OwinWebAPISelfHost { public class ValuesController : ApiController { // GET api/values public IEnumerable<string> Get() { return new string[] { "value1", "value2" }; } } }
Step4:设置监听端口
using Microsoft.Owin.Hosting; using System; using System.Net.Http; namespace OwinWebAPISelfHost { public class Program { static void Main() { 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(); } } } }
Step5:启动Console程序,这里我们看到返回的是Json格式的数据,当我们用火狐浏览器进行请求,会发现返回的是XML格式的数据,因为火狐浏览器默认的Accept为XML
相关文章推荐
- ASP.NET Web API与Owin OAuth:调用与用户相关的Web API(非第三方登录)
- ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API
- ASP.NET Web API与Owin OAuth:调用与用户相关的Web API(三)
- 使用 OWIN Self-Host ASP.NET Web API 2
- Asp.Net Web API 2第十课——使用OWIN自承载Web API
- Asp.Net Web API 2第十课——使用OWIN自承载Web API
- ASP.NET Web API与Owin OAuth:调用与用户相关的Web API
- Use OWIN to Self-Host ASP.NET Web API 2
- ASP.NET Web API实践系列01,以ASP.NET Web Form方式寄宿
- 第11章 ASP.NET Web API(WCF宿主+Web API、OWIN宿主+Web API)
- (转)【ASP.NET Web API】Authentication with OWIN
- [ASP.NET] 结合Web API在OWIN下实现OAuth
- 使用 OWIN Self-Host ASP.NET Web API 2
- Asp.Net Web API 2第十课——使用OWIN自承载Web API
- ASP.NET Web Api 实践系列(一)自我寄宿
- Use OWIN to Self-Host ASP.NET Web API 2 来访问我的webapi
- 具有 Windows Azure AD 和 Microsoft OWIN 组件的安全 ASP.NET Web API
- 使用 OWIN 作为 ASP.NET Web API 的宿主
- Asp.Net Web API 2第十课——使用OWIN自承载Web API
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证