C#ASP.NET核心web api httprequestmessage重写

编程入门 行业动态 更新时间:2024-10-27 08:32:12
本文介绍了C#ASP.NET核心web api httprequestmessage重写的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在从ASP.NET Web API重写一个POST方法到ASP.NET Core我不知道如何重写下面的代码。此代码在ASP.NET Web API中工作正常。 ASP.NET Web Api:

I am re-writing a POST method from ASP.NET Web API to ASP.NET Core I am not sure how to re-write the below code. This code works fine in ASP.NET Web API. ASP.NET Web Api:

public IHttpActionResult Post() { var rawds = ConvertByteToArray(Request.Content.ReadAsStreamAsync().Result); //error here at Content //do something } private byte[] ConvertByteToArray(Stream stream) { using (MemoryStream ms = new MemoryStream()) { stream.CopyTo(ms); return ms.ToArray(); } }

我的尝试: 当我在ASP.NET Core中编写如下相同的代码时,我得到以下错误'HttpRequest'不包含'Content'的定义,也没有扩展方法'Content'接受a可以找到类型'HttpRequest'的第一个参数(你是否缺少using指令或程序集引用?) ASP.NET核心:

What I have tried: When I write the same code as below in ASP.NET Core I get the following error 'HttpRequest' does not contain a definition for 'Content' and no extension method 'Content' accepting a first argument of type 'HttpRequest' could be found (are you missing a using directive or an assembly reference?) ASP.NET Core:

public IActionResult Post() { var rawds = ConvertByteToArrayAsync(Request.Content.ReadAsStreamAsync().Result); //do something } public static async Task<byte[]> ConvertByteToArrayAsync(Stream stream) { using (var ms = new MemoryStream(2048)) { await request.Body.CopyToAsync(ms); //get error here at request return ms.ToArray(); } }

推荐答案

await request.Body.CopyToAsync(ms);

1.无论如何,它应该是Request(R而不是r)! 2.似乎是一个逻辑错误,你将'Result'流传递给函数但不使用它?你可能想把它复制到ms?

1. In any case it should be Request (R and not r)! 2. It seems to be a logic error, that you pass the 'Result' stream to the function but do not use it? You probably meant to copy it to ms?

更多推荐

C#ASP.NET核心web api httprequestmessage重写

本文发布于:2023-10-29 13:14:15,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539788.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:重写   核心   NET   ASP   httprequestmessage

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!