Why might an ASP.NET Core raw WebSocket endpoint mapped with MapGet or [HttpGet] fail for browsers over HTTPS, and what fixes it?
answer
- the handshake method is not always GET
- HTTP/2 extended CONNECT
- Kestrel defaults to Http1AndHttp2
- app.Map or [Route] instead
- IIS: no WebSockets over HTTP/2
basics
~20 sOver HTTPS, Kestrel negotiates HTTP/2, where a browser opens a WebSocket with an extended CONNECT request, not a GET. A GET-only route never matches it; map the endpoint with app.Map or [Route] so any method reaches it.
solid answer
~40 sKestrel's endpoints default to `HttpProtocols.Http1AndHttp2`, and over TLS a browser that negotiated HTTP/2 opens the WebSocket with an extended CONNECT request (RFC 8441) instead of `GET` plus `Upgrade`. Kestrel and `WebSocketMiddleware` have handled this since .NET 7: `IsWebSocketRequest` is true for it and `AcceptWebSocketAsync` works unchanged. Routing is the catch. `MapGet` and `[HttpGet]` only match `GET`, so the CONNECT request never reaches the handler and routing answers 405 Method Not Allowed. Map the socket endpoint with `app.Map("/bids", ...)` or the action with `[Route("/bids")]`, and keep the `IsWebSocketRequest` check that turns other methods away. IIS does not support WebSockets over HTTP/2, so behind IIS the same browser falls back to HTTP/1.1.
code
csharp · 19 lines// Minimal API: Map matches both the HTTP/1.1 GET and the HTTP/2 CONNECT handshake.
app.Map("/bids", async (HttpContext context) =>
{
if (!context.WebSockets.IsWebSocketRequest) { context.Response.StatusCode = 400; return; }
using var socket = await context.WebSockets.AcceptWebSocketAsync();
await BidFeed.RunAsync(socket, context.RequestAborted);
});
// Controller: [Route] instead of [HttpGet] for the same reason.
public class BidSocketController : ControllerBase
{
[Route("/admin/bids")]
public async Task Connect()
{
if (!HttpContext.WebSockets.IsWebSocketRequest) { HttpContext.Response.StatusCode = 400; return; }
using var socket = await HttpContext.WebSockets.AcceptWebSocketAsync();
await BidFeed.RunAsync(socket, HttpContext.RequestAborted);
}
}go deeper
Remember that a browser on HTTPS may open the socket with CONNECT, so map the endpoint with app.Map or [Route] rather than MapGet or [HttpGet].
Explain that WebSocketMiddleware accepts both handshakes through IHttpUpgradeFeature and IHttpExtendedConnectFeature, so only routing needs attention.
Diagnose the 405-over-HTTPS symptom quickly and account for IIS and reverse proxies that do not carry HTTP/2 WebSockets.
Weigh HTTP/2 multiplexing of many sockets on one connection against the intermediaries in your path that may force HTTP/1.1.
## Two ways to open a WebSocket The original WebSocket handshake is an HTTP/1.1 `GET` with `Upgrade: websocket`, answered by `101 Switching Protocols`. HTTP/2 has no `Upgrade` mechanism, so RFC 8441 defined **extended CONNECT**: the client sends a `CONNECT` request with a `:protocol` pseudo-header of `websocket` on one HTTP/2 stream, and the server answers `200`. The WebSocket then lives on that stream, multiplexed with other requests on the same connection. The protocol detail belongs to the WebSocket protocol material; what matters in ASP.NET Core is **what this does to your code**. ## What ASP.NET Core handles for you - **Kestrel** supports WebSockets over HTTP/2 since .NET 7, and endpoints default to `HttpProtocols.Http1AndHttp2`, so over TLS (where browsers negotiate HTTP/2) it is in play without any configuration. - **`WebSocketMiddleware`** looks for either `IHttpUpgradeFeature` (HTTP/1.1) or `IHttpExtendedConnectFeature` (HTTP/2). For a CONNECT request with `:protocol = websocket` and a supported version, `IsWebSocketRequest` is `true`. - **`AcceptWebSocketAsync`** completes either handshake; you get the same `WebSocket` object and write the same loop. Browsers decide on their own: the docs note that Chrome, Edge and Firefox (128 and later) use HTTP/2 WebSockets by default when the server allows them. ## What breaks: method-specific routes Endpoints registered for one method only match that method: | Registration | Matches HTTP/1.1 `GET` handshake | Matches HTTP/2 `CONNECT` handshake | |---|---|---| | `app.MapGet("/bids", ...)` | yes | no | | `[HttpGet("/bids")]` on an action | yes | no | | `app.Map("/bids", ...)` | yes | yes | | `[Route("/bids")]` on an action | yes | yes | When a CONNECT request hits a path that only has a `GET` endpoint, routing's method matching produces **405 Method Not Allowed**, and the browser reports a failed WebSocket. The symptom is confusing because the same code works with a tool that speaks HTTP/1.1, and in development over plain HTTP, where browsers do not use HTTP/2. The ASP.NET Core docs give the fix for controllers directly: replace `[HttpGet("/path")]` with `[Route("/path")]`. For Minimal APIs the equivalent is `app.Map` (or `MapMethods` listing both `GET` and `CONNECT`). Because the endpoint now accepts any method, the `IsWebSocketRequest` check is what answers stray `POST`s with 400. ## Confirming which handshake you received When debugging, log two request properties before accepting: - `context.Request.Protocol` reads `HTTP/2` for an extended CONNECT handshake and `HTTP/1.1` for the classic upgrade; - `context.Request.Method` reads `CONNECT` or `GET` accordingly (`HttpMethods.IsConnect` tests it). Browser developer tools also show the protocol of the WebSocket request. If CONNECT requests appear in the server's logs as 405 responses with no handler running, the route registration is the culprit. ## Hosting caveats - **IIS** supports WebSockets but not WebSockets over HTTP/2, so browsers reaching the app through IIS use HTTP/1.1. - **Reverse proxies** in front of Kestrel must themselves support extended CONNECT to pass HTTP/2 WebSockets through; otherwise the browser negotiates HTTP/1.1 with the proxy. - Kestrel's HTTP/2 settings (protocol lists, limits per connection) are configured on Kestrel, not on `WebSocketOptions`. ## Why it matters HTTP/2 lets many WebSockets and ordinary requests share one TLS connection, and fixing the route is a one-word change. Teams usually discover the issue when a raw socket endpoint "works on my machine" over HTTP and fails behind HTTPS in staging.
- Does the WebSocket handler code itself need to change for HTTP/2?No. `IsWebSocketRequest`, `AcceptWebSocketAsync` and the `WebSocket` you get back behave the same; `WebSocketMiddleware` hides whether the handshake was an upgrade or an extended CONNECT. Only the route registration changes, from a GET-only mapping to one that also matches CONNECT. The `AllowedOrigins` check and keep-alive options apply to both.
- WebSockets only ever run over HTTP/1.1, so HTTP/2 cannot affect them.
- Keep MapGet; the WebSocket middleware rewrites CONNECT requests to GET.
- Enabling HTTP/2 WebSockets needs a new accept API on WebSocketManager.
- IIS serves WebSockets over HTTP/2 just like Kestrel.
Assumes ASP.NET Core 10; Kestrel and the WebSockets middleware have accepted HTTP/2 extended CONNECT WebSockets since .NET 7, so GET-only routes have missed them ever since.