不想在Startup全局生效,这个参数怎么传入添加呢?

goodgame365 2021-05-27 10:44:23
有个自定义模型绑定,需要
 services.AddMvc(options =>
{
//需要插入到第一条,内置默认是匹配到合适的Provider就不会在向下继续绑定;如果添加到末尾,即不会调用到我们实现的
options.ModelBinderProviders.Insert(0, new StringTrimModelBinderProvider(options.InputFormatters));
});


但这样,这个自定义设置就全局绑定了,而我只想在指定的模型实参里才有效,但是不知道 怎么传入这个options.InputFormatters,我写成这样,但不能识别,请高人指点,感谢!
 [ModelBinder(BinderType = typeof(StringTrimModelBinderProvider(options.InputFormatters)))]
public class TestModel
{

//....
}


补充下,这个StringTrimModelBinderProvider 类,在他的构造器里是需要传入
public StringTrimModelBinderProvider(IList<IInputFormatter> formatters)
{
_formatters = formatters;
}
...全文
284 1 打赏 收藏 转发到动态 举报
写回复
用AI写文章
1 条回复
切换为时间正序
请发表友善的回复…
发表回复
goodgame365 2021-05-27
  • 打赏
  • 举报
回复
引用 1 楼 wanghui0380 的回复:
[ModelBinder(BinderType = typeof(StringTrimModelBinderProvider))] 他是type,不是构造。 。。。。
第一种怎么用,赋值然后呢,怎么把他变成(IList<IInputFormatter>类型呢,因为后面需要用到他,原谅我比较菜,我贴这个类出来帮我看下吧
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Formatters;
using Microsoft.AspNetCore.Mvc.Infrastructure;
using Microsoft.AspNetCore.Mvc.ModelBinding;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.Options;
using System;
using System.Collections.Generic;

namespace Model
{
    public class StringTrimModelBinderProvider : IModelBinderProvider
    {
        private readonly IList<IInputFormatter> _formatters;

        public StringTrimModelBinderProvider(IList<IInputFormatter> formatters)
        {
            _formatters = formatters;
        }

        public IModelBinder GetBinder(ModelBinderProviderContext context)
        {


            var mvcoptions = context.Services.GetService<IOptions<MvcOptions>>();
            

            //添加需要自定义模型类名
            List<string> M = new List<string>();
            M.Add(typeof(TestModel).Name);
            M.Add(typeof(HouseModel).Name);
            M.Add(typeof(HouseBuildingModel).Name);
            M.Add(typeof(HouseRoomModel).Name);
            M.Add(typeof(HouseSourceModel).Name);
            M.Add(typeof(HouseTypeModel).Name);



            string ModelName =context.Metadata.ModelType.Name;
            if (context == null)
                throw new ArgumentNullException(nameof(context));
            if (!M.Contains(ModelName))
               return null;


            if (!context.Metadata.IsComplexType && context.Metadata.ModelType == typeof(string))
            {
                //简单类型
                var loggerFactory = (ILoggerFactory)context.Services.GetService(typeof(ILoggerFactory));
                return new SimpleStringTrimModelBinder(context.Metadata.ModelType);
            }
            else if (context.BindingInfo.BindingSource != null &&
                context.BindingInfo.BindingSource.CanAcceptDataFrom(BindingSource.Body))
            {
                //通过[FromBody]绑定的
                return new BodyStringTrimModelBinder(_formatters, context.Services.GetRequiredService<IHttpRequestStreamReaderFactory>());
            }
          
            return null;
        }
    }
}

110,549

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧