c# - ASPNetCore API IFormFile 返回 "invalid input"

标签 c# asp.net-core-2.0 asp.net-core-webapi

我有一个 API,我正在尝试上传图片。我尝试使用 swagger 和 postman。

我得到的只是

{
    "": [
        "The input was not valid."
    ]
}

这就是我的代码的样子 ->

Controller -

    [HttpPost("images")]
    public async Task<IActionResult> UploadImage(IFormFile file)
    {
        return await _imageHandler.UploadImage(file);
    }

图像处理程序 -

public interface IImageHandler
{
    Task<IActionResult> UploadImage(IFormFile file);
}

public class ImageHandler : IImageHandler
{
    private readonly IImageWriter _imageWriter;
    public ImageHandler(IImageWriter imageWriter)
    {
        _imageWriter = imageWriter;
    }

    public async Task<IActionResult> UploadImage(IFormFile file)
    {
        var result = await _imageWriter.UploadImage(file);
        return new ObjectResult(result);
    }
}

最后是图片作者

public class WriterHelper
{
    public enum ImageFormat
    {
        bmp,
        jpeg,
        gif,
        tiff,
        png,
        unknown
    }

    public static ImageFormat GetImageFormat(byte[] bytes)
    {
        var bmp = Encoding.ASCII.GetBytes("BM");     // BMP
        var gif = Encoding.ASCII.GetBytes("GIF");    // GIF
        var png = new byte[] { 137, 80, 78, 71 };              // PNG
        var tiff = new byte[] { 73, 73, 42 };                  // TIFF
        var tiff2 = new byte[] { 77, 77, 42 };                 // TIFF
        var jpeg = new byte[] { 255, 216, 255, 224 };          // jpeg
        var jpeg2 = new byte[] { 255, 216, 255, 225 };         // jpeg canon

        if (bmp.SequenceEqual(bytes.Take(bmp.Length)))
            return ImageFormat.bmp;

        if (gif.SequenceEqual(bytes.Take(gif.Length)))
            return ImageFormat.gif;

        if (png.SequenceEqual(bytes.Take(png.Length)))
            return ImageFormat.png;

        if (tiff.SequenceEqual(bytes.Take(tiff.Length)))
            return ImageFormat.tiff;

        if (tiff2.SequenceEqual(bytes.Take(tiff2.Length)))
            return ImageFormat.tiff;

        if (jpeg.SequenceEqual(bytes.Take(jpeg.Length)))
            return ImageFormat.jpeg;

        if (jpeg2.SequenceEqual(bytes.Take(jpeg2.Length)))
            return ImageFormat.jpeg;

        return ImageFormat.unknown;
    }
}

我正在学习本教程 -> https://www.codeproject.com/Articles/1256591/Upload-Image-to-NET-Core-2-1-API

我已经在项目中修改了Swagger可以上传文件,但是即使是swagger也会抛出同样的错误。 我的代码中没有其他错误,并且调试没有任何作用(尝试在我的 Controller 中设置断点,但显然他们不接受或不应该接受,仍在此处学习)。

任何帮助将不胜感激。谢谢!

enter image description here

做了一个干净的测试,问题似乎在 [ApiController] 中。知道为什么/如何修复它吗?

最佳答案

找到了我的问题并找到了解决方法。 在发现我的问题是 [ApiController] 之后,我偶然发现了另一个与此相关的 stackoverflow 问题。

这解决了我的问题。将 services.AddMvc() 修改为 -> services.AddMvc().SetCompatibilityVersion(CompatibilityVersion.Version_2_1);

关于c# - ASPNetCore API IFormFile 返回 "invalid input",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52523043/

相关文章:

c# - 将字符串拆分为字典

c# - ASP.NET Core CORS WebAPI : persist no Access-Control-Allow-Origin header

c# - 如何在 aspnet.core web api 中验证 JWT token ?

c# - 我如何遍历 List<T> 并获取每个项目?

c# - SQLite Studio生成不正确的DDL

razor - URL.Page 对 ASP.Core Identity 返回 null

c# - 如何在Asp.net core中将数据从AuthorizationHandler传递到Controller

.net - Razor 页面可以放在除 Pages 之外的任何其他目录中吗?

c# - EF Core 无法连接到服务器 - 与网络相关或特定于实例的错误

c# - 同步问题