本文介绍了IFormFile的Asp.Net Core摇摇欲坠的帮助页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试设置用于测试具有IFormFile属性的模型的工具.例如我有下一个api方法

I am trying to setup swagger for testing models which have IFormFile properties.For example I have next api method

[HttpPost]
public ApiResult<UserModel> SaveTestFileData([FromForm]TestPostFileArgs args)
{
    var result = new UserModel() { Id = 1, Name = $"SaveTestFileData {args.UserId} company: {args.CompanyId}, file length: {args.CompanyFile.Length}" };
    return ApiResult.Success(result);
}

还有我的参数模型

public class TestPostFileArgs
{
    public int UserId { get; set; }
    public int? CompanyId { get; set; }
    public IFormFile CompanyFile { get; set; }
}

默认情况下,摇摇欲坠生成帮助页面,不允许对其进行测试为了解决这个问题,我写了下一个OperationFilter

By default swagger generate help page which does not allow to test itTo solve this problem I wrote next OperationFilter

public class FormFileOperationFilter: IOperationFilter
{
    public void Apply(Operation operation, OperationFilterContext context)
    {
        if (operation.Parameters == null)
            return;

        var fileParamNames = context.ApiDescription.ActionDescriptor.Parameters
            .SelectMany(x => x.ParameterType.GetProperties())
            .Where(x => x.PropertyType.IsAssignableFrom(typeof (IFormFile)))
            .Select(x => x.Name)
            .ToList();
        if (!fileParamNames.Any())
            return;

        var paramsToRemove = new List<IParameter>();
        foreach (var param in operation.Parameters)
        {
            paramsToRemove.AddRange(from fileParamName in fileParamNames where param.Name.StartsWith(fileParamName + ".") select param);
        }
        paramsToRemove.ForEach(x => operation.Parameters.Remove(x));
        foreach (var paramName in fileParamNames)
        {
            var fileParam = new NonBodyParameter
                {
                    Type = "file",
                    Name = paramName,
                    In = "formData"
                };
            operation.Parameters.Add(fileParam);
        }
        foreach (IParameter param in operation.Parameters)
        {
            param.In = "formData";
        }

        operation.Consumes = new List<string>() { "multipart/form-data" };
    }
}

这一切如我所料,大摇大摆之后就可以了.

And after this everething works as I expect from swagger.

目前,此解决方案对我有用,但感觉不对.也许我为此缺少一些简单的解决方案.同样,这种方法不能使用IFormFile或其他方法来处理List或复杂对象的属性.

For now this solution works for me, but it feels not right. Maybe I am missing some simple solution for this. Also this approach does not handle List or complex object properties with IFormFile or maybe something else.

推荐答案

对于您的ASP.NET Core开发人员,在Swashbuckle.AspNetCore GitHub存储库中记录了以下问题: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/193 .注释中也有一些用于OperationFilter的工作代码-在这个问题中,一个对我来说比其他人工作得更好.

For you ASP.NET Core developers, there's an issue written up in the Swashbuckle.AspNetCore GitHub repo for this: https://github.com/domaindrivendev/Swashbuckle.AspNetCore/issues/193. It has some working code for an OperationFilter in the comments as well -- that one worked better for me than the other ones in this question.

这篇关于IFormFile的Asp.Net Core摇摇欲坠的帮助页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-18 10:03