.NET Core 2:如何检查请求是否为MIME多部分内容? [英] .NET Core 2: How to check if the request is MIME multipart Content?

查看:87
本文介绍了.NET Core 2:如何检查请求是否为MIME多部分内容?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

In ASp.NET MVC, you can check if the current request is MIME multipart content. How to check that in .NET Core? I have an action filter to validate that, but not sure how to evaluate to request header. IsMimeMultipartContent is not available in .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.");
    }
}

推荐答案

ASP.NET MVC Core使用 IFormFile 接口绑定多部分HTTP请求.如果请求不使用multipart/form-data,则文档指出绑定值将为空.

ASP.NET MVC Core uses the IFormFile interface to bind multipart HTTP requests. If the request does not use multipart/form-data, the documentation states that the bound value will be null.

这篇关于.NET Core 2:如何检查请求是否为MIME多部分内容?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆