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

标签 .net asp.net-core asp.net-core-2.0

在 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/

相关文章:

asp.net - <asp :PlaceHolder> alternate in mvc Razor page?

c# - 在单个 Web API 方法上弃用多个路由中的特定路由

c# - 未调用 CreateBehavior()

c# - Sharepoint 客户端对象模型设置 ModifiedBy 字段

c# - 在一台服务主机中托管多个服务?

c# - 从 ASP.Net Core API 方法中的 ActionResult<object> 获取值

debugging - 程序 '[3984] dotnet.exe' 已退出,代码为 -2147450751 (0x80008081)

.net - log4net.ThreadContext 和 log4net.LogicalThreadContext 有什么区别?

c# - 使用测试服务器的 .NET 6 E2E 测试在 TeamCity CI 上失败,但工作手动运行

c# - EF7 如何处理嵌套实体的更新操作