有個自定義模型系結,需要
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;
}
uj5u.com熱心網友回復:
[ModelBinder(BinderType = typeof(StringTrimModelBinderProvider))]他是type,不是構造。
系統查找查找的是type,不是構造。他內部其實是一個ModelBindeService.AddSingle(typeof(xxxx),xxxx)
所以你只需修改紅字部分即可
剩下部分,兩種方法
方法一:無需糾結那個構造,直接拿掉,在IModelBinderProvider
介面方法里直接寫var mvcoptions= context.Services.GetService<IOptions < MvcOptions >> ()
public IModelBinder GetBinder(ModelBinderProviderContext context)
{
var mvcoptions= context.Services.GetService<IOptions < MvcOptions >> //直接在這里獲取
}
方法2:
如果你實在非要糾結那個地方,一定要按你的想法寫,那么我們可以寫成
service.AddSingler<StringTrimModelBinderProvider>() //注冊一個單例物件
class StringTrimModelBinderProvider
{
StringTrimModelBinderProvider(IOptions<MvcOptions> mvcOptions) //把mvcoption注入進來
{
}
}
services.AddMvc(options =>
{
services.AddSingleton<StringTrimModelBinderProvider>();
services.AddControllers().AddMvcOptions(options => options.ModelBinderProviders.Insert(0, services.BuildServiceProvider().GetService<StringTrimModelBinderProvider>())); //看著稍微別扭點,其實可以放到后面app.userXXX部分去寫
});
ps:這一切還是在hosting部分的操作,還是他們那些人不想聽不想看的東西,想玩什么netcore,第一件事情請學好Hosting部分,學不會這塊妄談什么EF,Razor,datatable,abp,微軟開倒車。學不會這塊,整個netcore都是瞎的。弄清楚這塊,netcore你想怎么玩都行
uj5u.com熱心網友回復:
第一種怎么用,賦值然后呢,怎么把他變成(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;
}
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/net/285324.html
標籤:C#
