C# 从ASP.NET Core中的WebSocket请求返回什么IActionResult?

C# 从ASP.NET Core中的WebSocket请求返回什么IActionResult?,c#,asp.net-core,websocket,asp.net-core-mvc,C#,Asp.net Core,Websocket,Asp.net Core Mvc,我有一个ASP.NET Core 2.1 webapi正在运行,它正在侦听web套接字请求。如果请求不是为了WebSocket升级,我想返回这个.BadRequest() [Route("api/[controller]")] [ApiController] public class SocketController : ControllerBase { [HttpGet] public async Task<IActionResult> Post() {

我有一个ASP.NET Core 2.1 webapi正在运行,它正在侦听web套接字请求。如果请求不是为了WebSocket升级,我想
返回这个.BadRequest()

[Route("api/[controller]")]
[ApiController]
public class SocketController : ControllerBase
{
    [HttpGet]
    public async Task<IActionResult> Post()
    {
        if (!this.HttpContext.WebSockets.IsWebSocketRequest)
            return this.BadRequest();

        using (var webSocket = await this.HttpContext.WebSockets.AcceptWebSocketAsync())
        {
            using (var jsonRpc = new JsonRpc(new WebSocketMessageHandler(webSocket)))
            {
                jsonRpc.AddLocalRpcTarget(new SocketServer());
                jsonRpc.StartListening();
                await jsonRpc.Completion;
            }
        }

        return this.Ok();
    }
}

秘密是
EmptyResult
,没有
this.Empty()
方法

public async Task<IActionResult> Post()
{
    if (!this.HttpContext.WebSockets.IsWebSocketRequest)
    {
        return this.BadRequest();
    }

    using (var webSocket = await this.HttpContext.WebSockets.AcceptWebSocketAsync())
    {
        using (var jsonRpc = new JsonRpc(new WebSocketMessageHandler(webSocket)))
        {
            jsonRpc.AddLocalRpcTarget(new SocketServer());
            jsonRpc.StartListening();
            await jsonRpc.Completion;
        }
    }

    return new EmptyResult();
}
public async Task Post()
{
如果(!this.HttpContext.WebSockets.IsWebSocketRequest)
{
返回此.BadRequest();
}
使用(var webSocket=wait this.HttpContext.WebSockets.AcceptWebSocketAsync())
{
使用(var jsonRpc=newjsonrpc(newwebsocketmessagehandler(webSocket)))
{
AddLocalRpcTarget(新的SocketServer());
jsonRpc.StartListening();
等待jsonRpc.Completion;
}
}
返回新的EmptyResult();
}

问题在于websocket处理程序可能已经发送了一些信息,以便进行通信。我想有一种方法可以使用当前的HttpContext来提供响应,而不是通过函数返回来提供响应。不过我对asp.net一无所知。很可能是由于套接字请求,管道中更高层的中间件已经开始响应。应该开始调查了,谢谢,@nkosi。我的中间件非常简单。我已经把它添加到我的问题中了。
public async Task<IActionResult> Post()
{
    if (!this.HttpContext.WebSockets.IsWebSocketRequest)
    {
        return this.BadRequest();
    }

    using (var webSocket = await this.HttpContext.WebSockets.AcceptWebSocketAsync())
    {
        using (var jsonRpc = new JsonRpc(new WebSocketMessageHandler(webSocket)))
        {
            jsonRpc.AddLocalRpcTarget(new SocketServer());
            jsonRpc.StartListening();
            await jsonRpc.Completion;
        }
    }

    return new EmptyResult();
}