将API从.NET框架重写为.netCore,获取System.notsupportedException'尝试获取文件流

发布于 2025-01-27 15:01:16 字数 1322 浏览 3 评论 0 原文

我将一个项目迁移到.NET Core MVC,

  1. 以前的代码具有用于上传文档的API,将
  2. 上述代码重写的同一代码重写到下面的代码,使用的是“使用” Microsoft.aspnetcore.mvc.webapicompatshim“要获得httprequestmessage

[旧代码:在.net框架中工作正常]

public HttpResponseMessage UploadFile(string filetype)//old code
    {
        Stream file = Request.Content.ReadAsStreamAsync().Result;
        string id;
        id = DocumentHelper.SaveDocument(filetype, file);

        return Request.CreateResponse<string>(HttpStatusCode.OK, id);
    }


public  HttpResponseMessage UploadFile(string filetype)//Re-Written Code
    {
        HttpRequestMessageFeature requestMessage = new HttpRequestMessageFeature(Request.HttpContext);
        HttpRequestMessage httpRequestMessage = requestMessage.HttpRequestMessage;
        Stream file = httpRequestMessage.Content.ReadAsStreamAsync().Result;
        string id;
        id = DocumentHelper.SaveDocument(filetype, file);

        return httpRequestMessage.CreateResponse<string>(HttpStatusCode.OK, id);
    }

[1]:https://i.sstatic.net /cc4af.png

尝试使用request.body与流读取器,并启用了

任何指针 。返回类型

I am Migrating one of projects to .NET CORE MVC ,

  1. Previous code had API to Upload document, re-written same
  2. Rewritten Above Code to below code , used "using
    Microsoft.AspNetCore.Mvc.WebApiCompatShim" to get HttpRequestMessage

[Older Code: Working Fine in .NET FRAMEWORK]

public HttpResponseMessage UploadFile(string filetype)//old code
    {
        Stream file = Request.Content.ReadAsStreamAsync().Result;
        string id;
        id = DocumentHelper.SaveDocument(filetype, file);

        return Request.CreateResponse<string>(HttpStatusCode.OK, id);
    }


public  HttpResponseMessage UploadFile(string filetype)//Re-Written Code
    {
        HttpRequestMessageFeature requestMessage = new HttpRequestMessageFeature(Request.HttpContext);
        HttpRequestMessage httpRequestMessage = requestMessage.HttpRequestMessage;
        Stream file = httpRequestMessage.Content.ReadAsStreamAsync().Result;
        string id;
        id = DocumentHelper.SaveDocument(filetype, file);

        return httpRequestMessage.CreateResponse<string>(HttpStatusCode.OK, id);
    }

Quickwatch for ReWritten Code when trying to read content

[1]: https://i.sstatic.net/cc4AF.png

Tried using Request.Body with stream reader, and enabled buffering.

Any Pointers How to read the stream from Microsoft.AspNetCore.HTTP.HttpRequest,I AM TRYING TO AVOID async function that would force me to change return type

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文