小编Jon*_*han的帖子

Asp .Net Core中的自定义模型绑定

我正在尝试将具有IFormFileIFormFileCollection属性的模型绑定到我的自定义类CommonFile.我没有在互联网上找到这么多关于它使用asp .net核心的文档,我试图在ASP.Net Core 1.0中关注此链接自定义模型绑定, 但它绑定了一个SimpleType属性,我需要绑定一个复杂的类型.无论如何,我试图使我的这个绑定版本,我有以下代码:

FormFileModelBinderProvider.cs

public class FormFileModelBinderProvider : IModelBinderProvider
{
    public IModelBinder GetBinder(ModelBinderProviderContext context)
    {
        if (context == null) throw new ArgumentNullException(nameof(context));

        if (!context.Metadata.IsComplexType) return null;

        var isIEnumerableFormFiles = context.Metadata.ModelType.GetInterfaces().Contains(typeof(IEnumerable<CommonFile>));

        var isFormFile = context.Metadata.ModelType.IsAssignableFrom(typeof(CommonFile));

        if (!isFormFile && !isIEnumerableFormFiles) return null;

        var propertyBinders = context.Metadata.Properties.ToDictionary(property => property,
            context.CreateBinder);
        return new FormFileModelBinder(propertyBinders);
    }
}
Run Code Online (Sandbox Code Playgroud)

FromFileModelBinder.cs

下面的代码是不完整的,因为我没有得到任何结果,bindingContext.ValueProvider.GetValue(bindingContext.ModelName);而我正在调试一切顺利,直到bindingContext.ModelName没有值,我无法绑定我的模型从httpContext到强类型模型.

public class FormFileModelBinder : IModelBinder
{
    private readonly ComplexTypeModelBinder _baseBinder;

    public FormFileModelBinder(IDictionary<ModelMetadata, IModelBinder> …
Run Code Online (Sandbox Code Playgroud)

.net c# asp.net-core-mvc asp.net-core

7
推荐指数
1
解决办法
3978
查看次数

标签 统计

.net ×1

asp.net-core ×1

asp.net-core-mvc ×1

c# ×1