ASP.NET 核心:
- 支援 .NET 的開放式 Web 介面 (OWIN)。
- 具有與
Microsoft.Owin.*(Katana) 程式庫相容的替代方案。
OWIN 可讓 Web 應用程式獨立於網頁伺服器。 它會定義中介軟體要在管線中用來處理要求和相關聯回應的標準方式。 ASP.NET Core 應用程式和中介軟體可以與以 OWIN 為基礎的應用程式、伺服器及中介軟體進行交互操作。
OWIN 提供分離層,可讓兩個利用不同物件模型的架構一起使用。
Microsoft.AspNetCore.Owin 套件提供兩個配接器實作:
- ASP.NET Core 至 OWIN
- OWIN 至 ASP.NET Core
這可讓 ASP.NET Core 裝載在 OWIN 相容的伺服器/主機之上,或讓其他 OWIN 相容的元件在 ASP.NET Core 之上執行。
Note
使用這些配接器將伴隨效能成本增加。 僅使用 ASP.NET Core 元件的應用程式不應使用 Microsoft.AspNetCore.Owin 套件或配接器。
在 ASP.NET Core 管線中執行 OWIN 中介軟體
ASP.NET Core 的 OWIN 支援部署為 Microsoft.AspNetCore.Owin 套件的一部分。 您可以藉由安裝此套件,將 OWIN 支援匯入您的專案中。
OWIN 中間件符合 OWIN 規格,這需要 Func<IDictionary<string, object>, Task> 介面,而且必須設定特定的密鑰(例如 owin.ResponseBody)。 下列簡單的 OWIN 中介軟體會顯示 "Hello World":
public Task OwinHello(IDictionary<string, object> environment)
{
string responseText = "Hello World via OWIN";
byte[] responseBytes = Encoding.UTF8.GetBytes(responseText);
// OWIN Environment Keys: http://owin.org/spec/spec/owin-1.0.0.html
var responseStream = (Stream)environment["owin.ResponseBody"];
var responseHeaders = (IDictionary<string, string[]>)environment["owin.ResponseHeaders"];
responseHeaders["Content-Length"] = new string[] { responseBytes.Length.ToString(CultureInfo.InvariantCulture) };
responseHeaders["Content-Type"] = new string[] { "text/plain" };
return responseStream.WriteAsync(responseBytes, 0, responseBytes.Length);
}
範例簽章會傳回 Task,並依照 OWIN 的要求接受 IDictionary<string, object>。
下列程式碼示範如何使用 OwinHello 擴充方法,將 UseOwin 中介軟體 (如上所示) 新增至 ASP.NET Core 管線。
public void Configure(IApplicationBuilder app)
{
app.UseOwin(pipeline =>
{
pipeline(next => OwinHello);
});
}
您可以設定在 OWIN 管線內進行其他動作。
Note
只能在第一次寫入回應資料流之前修改回應標頭。
Note
基於效能考量,建議您不要多次呼叫 UseOwin。 OWIN 元件如果群組在一起,其運作效能最佳。
app.UseOwin(pipeline =>
{
pipeline(next =>
{
return async environment =>
{
// Do something before.
await next(environment);
// Do something after.
};
});
});
OWIN 環境
您可以使用 HttpContext 建構 OWIN 環境。
var environment = new OwinEnvironment(HttpContext);
var features = new OwinFeatureCollection(environment);
OWIN 金鑰
OWIN 仰賴 IDictionary<string,object> 物件在 HTTP 要求/回應交換中傳遞資訊。 ASP.NET Core 會實作下面所列的索引鍵。 請參閱 主要規格、延伸模組和 OWIN 金鑰指導方針和通用密鑰。
要求資料 (OWIN 1.0.0 版)
| Key | 值 (類型) | Description |
|---|---|---|
| owin.RequestScheme | String |
|
| owin.RequestMethod | String |
|
| owin.RequestPathBase | String |
|
| owin.RequestPath | String |
|
| owin.RequestQueryString | String |
|
| owin.RequestProtocol | String |
|
| owin.RequestHeaders | IDictionary<string,string[]> |
|
| owin.RequestBody | Stream |
要求資料 (OWIN 1.1.0 版)
| Key | 值 (類型) | Description |
|---|---|---|
| owin.RequestId | String |
Optional |
回應資料 (OWIN 1.0.0 版)
| Key | 值 (類型) | Description |
|---|---|---|
| owin.ResponseStatusCode | int |
Optional |
| owin.ResponseReasonPhrase | String |
Optional |
| owin.ResponseHeaders | IDictionary<string,string[]> |
|
| owin.ResponseBody | Stream |
其他資料 (OWIN 1.0.0 版)
| Key | 值 (類型) | Description |
|---|---|---|
| owin.CallCancelled | CancellationToken |
|
| owin.Version | String |
常用鍵
| Key | 值 (類型) | Description |
|---|---|---|
| ssl.ClientCertificate | X509Certificate |
|
| ssl.LoadClientCertAsync | Func<Task> |
|
| server.RemoteIpAddress | String |
|
| server.RemotePort | String |
|
| server.LocalIpAddress | String |
|
| server.LocalPort | String |
|
| server.OnSendingHeaders | Action<Action<object>,object> |
發送文件 v0.3.0
| Key | 值 (類型) | Description |
|---|---|---|
| sendfile.SendAsync | 請參閱 委派簽章 | 每個請求 |
不透明 v0.3.0
| Key | 值 (類型) | Description |
|---|---|---|
| opaque.Version | String |
|
| opaque.Upgrade | OpaqueUpgrade |
請參閱 委派簽章 |
| opaque.Stream | Stream |
|
| opaque.CallCancelled | CancellationToken |
WebSocket v0.3.0 版
| Key | 值 (類型) | Description |
|---|---|---|
| websocket.Version | String |
|
| websocket.Accept | WebSocketAccept |
請參閱 委派簽章 |
| websocket.AcceptAlt | Non-spec | |
| websocket.SubProtocol | String |
請參閱 RFC6455節 4.2.2 步驟 5.5 |
| websocket.SendAsync | WebSocketSendAsync |
請參閱 委派簽章 |
| websocket.ReceiveAsync | WebSocketReceiveAsync |
請參閱 委派簽章 |
| websocket.CloseAsync | WebSocketCloseAsync |
請參閱 委派簽章 |
| websocket.CallCancelled | CancellationToken |
|
| websocket.ClientCloseStatus | int |
Optional |
| websocket.ClientCloseDescription | String |
Optional |
其他資源
- 請參閱轉譯層中支援的 OWIN 金鑰 GitHub 上的來源 。
- Middleware
- Servers