Owin 自寄宿 asp.net web api
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
Owin 自寄宿 asp.net web api的更多相关文章
- Use OWIN to Self-Host ASP.NET Web API 2
Open Web Interface for .NET (OWIN)在Web服务器和Web应用程序之间建立一个抽象层.OWIN将网页应用程序从网页服务器分离出来,然后将应用程序托管于OWIN的程序 ...
- Use OWIN to Self-Host ASP.NET Web API 2 来访问我的webapi
就是说我们本地的http://localhost:49708/api/test可以通过 这个东西来访问(懒得挂载iis,当然它的强大可不这些,由于测试出了问题 出记录一下) 首先去Nuget包里找到M ...
- 使用 OWIN Self-Host ASP.NET Web API 2
Open Web Interface for .NET (OWIN)在Web服务器和Web应用程序之间建立一个抽象层.OWIN将网页应用程序从网页服务器分离出来,然后将应用程序托管于OWIN的程序而离 ...
- (转)【ASP.NET Web API】Authentication with OWIN
概述 本文说明了如何使用 OWIN 来实现 ASP.NET Web API 的验证功能,以及在客户端与服务器的交互过程中,避免重复提交用户名和密码的机制. 客户端可以分为两类: JavaScript: ...
- ASP.NET Web API 安全筛选器
原文:https://msdn.microsoft.com/zh-cn/magazine/dn781361.aspx 身份验证和授权是应用程序安全的基础.身份验证通过验证提供的凭据来确定用户身份,而授 ...
- ASP.NET Web API与Owin OAuth:使用Access Toke调用受保护的API
在前一篇博文中,我们使用OAuth的Client Credential Grant授权方式,在服务端通过CNBlogsAuthorizationServerProvider(Authorization ...
- 使用 OWIN 作为 ASP.NET Web API 的宿主
使用 OWIN 作为 ASP.NET Web API 的宿主 ASP.NET Web API 是一种框架,用于轻松构建可以访问多种客户端(包括浏览器和移动 设备)的 HTTP 服务. ASP.NET ...
- 购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证
原文:购物车Demo,前端使用AngularJS,后端使用ASP.NET Web API(3)--Idetity,OWIN前后端验证 chsakell分享了前端使用AngularJS,后端使用ASP. ...
- [转] JSON Web Token in ASP.NET Web API 2 using Owin
本文转自:http://bitoftech.net/2014/10/27/json-web-token-asp-net-web-api-2-jwt-owin-authorization-server/ ...
随机推荐
- javascript常见内存泄露
一.全局变量引起的内存泄漏 function func(){ lmw = 123456 //lmw是全局变量,不会被释放 } 二.闭包引起的内存泄漏 function func(){ var lmw ...
- Paper | Residual learning
目录 1. OVERVIEW 2. DEGRADATION 3. SOLUTION(DEEP RESIDUAL LEARNING) 4. IMPLEMENTATION(SHORTCUT CONNECT ...
- java实现随机产生6位数的方法总结
package com.yin.test; import java.util.Random; import org.junit.Test; /** * @author v_yinyl * @date ...
- 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据
[源码下载] 背水一战 Windows 10 (89) - 文件系统: 读写文本数据, 读写二进制数据, 读写流数据 作者:webabcd 介绍背水一战 Windows 10 之 文件系统 读写文本数 ...
- 札记:翻译-使用Scene和Transition实现【场景切换】动画效果
简述:transitions framework 下面翻译transition为"过渡",强调动画过程的含义,不过更多时候使用transition单词本身. Android 4.4 ...
- [wxgl] load image fail
微信小游戏 let loader = new THREE.TextureLoader() let texture = loader.load('./images/bullet.png') 报错 参考 ...
- MongoDB 用法入门(windows)①
概述 大家对数据库肯定不陌生,肯定也有很多人用过MySQL,但是在用MySQL的时候各种建表,写表之间的关联让人非常头疼. MongoDB也是一种数据库,但是它不是用表,而是用集合来装数据的,我对这种 ...
- Swift5 语言指南(十) 枚举
一个枚举定义了一个通用型的一组相关的值,使你在你的代码中的一个类型安全的方式这些值来工作. 如果您熟悉C,您将知道C枚举将相关名称分配给一组整数值.Swift中的枚举更灵活,并且不必为枚举的每个案例提 ...
- TI 开发板安装USB转串口驱动
使用TI开发板的时候,USB转串口驱动没有,显示,无法识别设备.搜了好久才搜到相关驱动. 做个记录. 链接: https://pan.baidu.com/s/1ZT5zzVcU727jrYacKVoT ...
- postgresql-分页数据重复问题探索
postgresql-分页数据重复探索 问题背景 许多开发和测试人员都可能遇到过列表的数据翻下一页的时候显示了上一页的数据,也就是翻页会有重复的数据. 如何处理? 这个问题出现的原因是因为选择的排序字 ...