gpt4 book ai didi

.NET 核心 2 : How to check if the request is MIME multipart Content?

转载 作者:行者123 更新时间:2023-12-04 16:29:18 25 4
gpt4 key购买 nike

在 ASp.NET MVC 中,可以检查当前请求是否为 MIME 多部分内容。如何在 .NET Core 中检查?我有一个 Action 过滤器来验证这一点,但不确定如何评估请求 header 。 IsMimeMultipartContent 在 .NET Core 中不可用。

/// <summary>
/// Checks whether the current request specified content is MIME multipart content.
/// </summary>
/// <exception cref="HttpRequestException">Raised when the current request doesn't have MIME multipart content.</exception>
public class HasMimeMultipartContentAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext context)
{
if (!context.HttpContext.Request.Headers.IsMimeMultipartContent())
throw new HttpRequestException("This request does not contain any file to upload.");
}
}

最佳答案

您可以使用属性 HttpRequest.HasFormContentType 检查请求是多部分/表单数据。

然后使用属性 HttpRequest.Form.Files 在请求中存在文件

例子:

public async Task Invoke(HttpContext context)
{

if (context.Request.HasFormContentType && context.Request.Form.Files.Any)
{

}
}

关于.NET 核心 2 : How to check if the request is MIME multipart Content?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45871479/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com