IFormFile不使用已弃用的Microsoft.AspNetCore.Http.Features

huangapple go评论95阅读模式
英文:

IFormFile without deprecated Microsoft.AspNetCore.Http.Features

问题

我在类库中使用IFormFileMicrosoft.AspNetCore.Http.Features已经被弃用。现在在.NET 7中,我需要什么NuGet包,用于IFormFile

英文:

I use IFormFile in class library, Microsoft.AspNetCore.Http.Features has become deprecated. What kind nugget package I need now, for IFormFile in .Net 7

答案1

得分: 2

在.NET 7中,IFormFile 已经默认自动集成了。此外,如果您在 流式传输任何文件操作 中使用 IFormFile,则无需安装额外的包,因为 IFormFile 属于 Microsoft.AspNetCore.Http.Features.dll 程序集和 Microsoft.AspNetCore.Http 命名空间已经包含在 .NET 7 中。因此,您不需要安装或包含任何额外的 NuGet 包。

您可以查看以下信息:

如何在类库中使用:

为了在 .NET 7 中在类库中使用 IFormFile,我们可以使用 Microsoft.AspNetCore.Http NuGet 包。虽然它已经超出了支持范围,但仍然可以使用,因为 asp.netcore 2.2 被视为独特的类别并继续保留其传统。

示例:

namespace IFromFileTestClassLibrary
{
    public class UploadFiles
    {
        public async Task<string> FileHandlerMethod(CreateProfileViewModel requestModel)
        {
            string filePath = "文件保存路径";
            var path = Path.Combine(filePath, "Images", requestModel.Photo!.FileName);
            if (requestModel.Photo.Length > 0)
            {
                using var stream = new FileStream(path, FileMode.Create);
                await requestModel.Photo.CopyToAsync(stream);
            }
            return requestModel.ImageURL = path;
        }
    }

    public class CreateProfileViewModel
    {
        public string? ProfileName { get; set; }
        public string? DisplayPhoto { get; set; }
        public string? ImageURL { get; set; }
        public IFormFile? Photo { get; set; }
    }
}

注意: 您可以参考此官方文档获取更多详细信息。

英文:

> I use IFormFile in class library, Microsoft.AspNetCore.Http.Features
> has become deprecated. What kind nugget package I need now, for
> IFormFile in .Net 7

Actually, in .NET 7 IFormFile has been integrated automatically by default. In addition, if you use IFormFile for streaming any file operation you no longer required additional package because IFormFile which is belongs to Microsoft.AspNetCore.Http.Features.dll assembly and Microsoft.AspNetCore.Http namaspace has been included within .Net 7. Thus, you don't need to install or include any additional nuget package for that.

You can have a look at following information:

IFormFile不使用已弃用的Microsoft.AspNetCore.Http.Features

How To Use In Class Library:

In order to use IFormFile within class library within .NET 7 we can use Microsoft.AspNetCore.Http nuget package. Although its out of support scope but still be usuable as asp.netcore 2.2 considered as unique category and continued its legacy.

IFormFile不使用已弃用的Microsoft.AspNetCore.Http.Features

Example:

namespace IFromFileTestClassLibrary
{
    public class UploadFiles
    {
        public async Task&lt;string&gt; FileHandelerMethod(CreateProfileViewModel requestModel)
        {
            string filePath = &quot;file saving path&quot;;
            var path = Path.Combine(filePath, &quot;Images&quot;, requestModel.Photo!.FileName);
            if (requestModel.Photo.Length &gt; 0)
            {
                using var stream = new FileStream(path, FileMode.Create);
                await requestModel.Photo.CopyToAsync(stream);
            }
            return requestModel.ImageURL = path;
        }
    }


    public class CreateProfileViewModel
    {
        public string? ProfileName { get; set; }
        public string? DisplayPhoto { get; set; }
        public string? ImageURL { get; set; }
        public IFormFile? Photo { get; set; }
    }
}

Note: You can refer to this official document for more details.

答案2

得分: 2

请阅读此文档:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/target-aspnetcore

在 .csproj 文件中删除任何类似 Microsoft.Extensions.Features 的包,并改为引用框架。

<ItemGroup>
  <FrameworkReference Include="Microsoft.AspNetCore.App" />
</ItemGroup>
英文:

Please read this document:

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/target-aspnetcore

Delete any packages like Microsoft.Extensions.Features in the .csproj file and instead reference the framework.

&lt;ItemGroup&gt;
  &lt;FrameworkReference Include=&quot;Microsoft.AspNetCore.App&quot; /&gt;
&lt;/ItemGroup&gt;

huangapple
  • 本文由 发表于 2023年6月15日 19:48:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/76482165.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定