ASP.NET Core:
- 支援 .NET 的開放式 Web 介面 (OWIN)。
- 具有與 .NET Core 相容的替代方案,用於
Microsoft.Owin.*
函式庫。
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 之上執行。
注意
使用這些配接器將伴隨效能成本增加。 僅使用 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 管線內進行其他動作。
注意
只能在第一次寫入回應資料流之前修改回應標頭。
注意
基於效能考量,建議您不要多次呼叫 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 版)
按鍵 | 值 (類型) | 描述 |
---|---|---|
owin。RequestScheme | String |
|
owin。RequestMethod | String |
|
歐文。請求路徑Base | String |
|
owin。RequestPath | String |
|
owin。RequestQueryString | String |
|
owin。RequestProtocol | String |
|
owin。RequestHeaders | IDictionary<string,string[]> |
|
歐文。請求體 | Stream |
要求資料 (OWIN 1.1.0 版)
按鍵 | 值 (類型) | 描述 |
---|---|---|
歐文。請求ID | String |
選擇性 |
回應資料 (OWIN 1.0.0 版)
按鍵 | 值 (類型) | 描述 |
---|---|---|
owin。ResponseStatusCode | int |
選擇性 |
歐文。回應ReasonPhrase | String |
選擇性 |
owin。ResponseHeaders | IDictionary<string,string[]> |
|
owin。ResponseBody | Stream |
其他資料 (OWIN 1.0.0 版)
按鍵 | 值 (類型) | 描述 |
---|---|---|
owin。CallCancelled | CancellationToken |
|
歐文。版本 | String |
共同索引鍵
按鍵 | 值 (類型) | 描述 |
---|---|---|
SSL 的客戶證書 | X509Certificate |
|
SSL 的LoadClientCertAsync | Func<Task> |
|
伺服器。RemoteIpAddress | String |
|
伺服器。RemotePort | String |
|
伺服器。LocalIpAddress | String |
|
伺服器。LocalPort | String |
|
伺服器。OnSendingHeaders | Action<Action<object>,object> |
SendFiles 0.3.0 版
按鍵 | 值 (類型) | 描述 |
---|---|---|
send檔。SendAsync | 請參閱 委派簽章 | 每個要求 |
Opaque 0.3.0 版
按鍵 | 值 (類型) | 描述 |
---|---|---|
不透明。版本 | String |
|
不透明。升級 | OpaqueUpgrade |
請參閱 委派簽章 |
不透明流 | Stream |
|
不透明。通話已取消 | CancellationToken |
WebSocket 0.3.0 版
按鍵 | 值 (類型) | 描述 |
---|---|---|
websocket版本 | String |
|
websocket。接受 | WebSocketAccept |
請參閱 委派簽章 |
websocket。AcceptAlt | 非規格 | |
websocket 的子協定 | String |
請參閱 RFC6455節 4.2.2 步驟 5.5 |
websocket 的SendAsync | WebSocketSendAsync |
請參閱 委派簽章 |
websocket 的接收異步 | WebSocketReceiveAsync |
請參閱 委派簽章 |
websocket.CloseAsync | WebSocketCloseAsync |
請參閱 委派簽章 |
websocket。CallCancelled | CancellationToken |
|
websocket 的ClientCloseStatus | int |
選擇性 |
websocket:ClientCloseDescription | String |
選擇性 |
其他資源
- 請參閱轉譯層中支援的 OWIN 金鑰 GitHub 上的來源 。
- 中間件
- 伺服器