Skip to content

Add HttpConnectAttribute #43501

@BrennanConroy

Description

@BrennanConroy

Background and Motivation

We recently added WebSockets over HTTP/2 support to Kestrel. By default this lights up automatically if your app supports HTTP/2 and you use the browser. One potential problem is that using WebSockets with controllers could potentially stop working because WebSockets over HTTP/2 now uses a CONNECT request instead of a GET request.

Our docs even show using WebSockets from a controller and are now broken in this specific scenario.

Proposed API

namespace Microsoft.AspNetCore.Mvc;

+ public class HttpConnectAttribute : HttpMethodAttribute
+ {
+    public HttpConnectAttribute();

+    public HttpConnectAttribute([StringSyntax("Route")] string template);
+ }

Usage Examples

public class WebSocketController : ControllerBase
{
    [HttpGet("/ws")]
    [HttpConnect("/ws")]
    public async Task Get()
    {
        if (HttpContext.WebSockets.IsWebSocketRequest)
        {
            using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
            await Echo(webSocket);
        }
        else
        {
            HttpContext.Response.StatusCode = StatusCodes.Status400BadRequest;
        }
    }
}

Alternative Designs

public class WebSocketController : ControllerBase
{
    [WebSocket("/ws")]
    public async Task Get()
    {
        using var webSocket = await HttpContext.WebSockets.AcceptWebSocketAsync();
        await Echo(webSocket);
    }
}

Don't add a new API and add docs to show using a custom HttpMethodAttribute.

Risks

Metadata

Metadata

Assignees

Labels

DocsThis issue tracks updating documentationold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions