Home > Net >  Don't want to come into force in global Startup, how to add this parameter?
Don't want to come into force in global Startup, how to add this parameter?

Time:05-28

A custom binding model, need
 services. AddMvc (options=& gt; 
{
//need to insert into the first article, built-in default is matched to the appropriate Provider will not continue to bind down; If you add to the end, that is, don't call to our implementation
Options. ModelBinderProviders. Insert (0, new StringTrimModelBinderProvider (options. InputFormatters));
});


But in this way, the custom setting global bindings, and I just want to in the specified model is valid in the argument, but don't know how to pass in the options. InputFormatters, I write this, but cannot identify, please tall person to give directions, thank!
 [ModelBinder (BinderType=typeof (StringTrimModelBinderProvider (options. InputFormatters)))) 
Public class TestModel
{

//...
}


Complement, this StringTrimModelBinderProvider classes, in his the constructor is need to
Public StringTrimModelBinderProvider (IList Formatters)
{
_formatters=formatters;
}

CodePudding user response:

[ModelBinder (BinderType=typeof (StringTrimModelBinderProvider)]
He is the type, not construct,


System for lookup type, not construct, he is actually a ModelBindeService. Internal AddSingle (typeof (XXXX), XXXX)

So you only need to modify the scarlet letter part can


The remaining part, two methods

Method one: don't need to entanglements that structure, removed, directly on the IModelBinderProvider
Interface methods to write directly in the var mvcoptions=context. Services. GetService ()

Public IModelBinder GetBinder (ModelBinderProviderContext context)
{
Var mvcoptions=context. Services. GetService//directly get here

}

Method 2:
If you really have to struggle the place, be sure to write according to your idea, then we can be written as

Service. AddSingler ()//registered a singleton

The class StringTrimModelBinderProvider
{
StringTrimModelBinderProvider (IOptions MvcOptions)//bring mvcoption injection in
{
}
}

Services. AddMvc (options=& gt;
{
Services. AddSingleton (a);
Services. AddControllers (.) AddMvcOptions (options=& gt; Options. ModelBinderProviders. Insert (0, services. BuildServiceProvider () GetService ()));//look at a slightly awkward, actually can be placed behind the app. UserXXX part to write
});



Ps: all this hosting part of the operation, or they don't want to listen to those people don't want to see, want to play what netcore, first thing please learn hosting part, not the jump about EF, Razor, datatable, abp, Microsoft back, at this, the whole netcore is blind, how would you like to clear this, netcore play will do


CodePudding user response:

reference 1st floor wanghui0380 response:
[ModelBinder (BinderType=typeof (StringTrimModelBinderProvider)]
He is the type, not construct,

,,,,,,


How to use the first assignment and then what, how to make him (IList Type, because need behind him, forgive me more food, I posted this class help me see
 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. The Options;
using System;
using System.Collections.Generic;

The namespace Model
{
Public class StringTrimModelBinderProvider: IModelBinderProvider
{
Private readonly IList _formatters;

Public StringTrimModelBinderProvider (IList Formatters)
{
_formatters=formatters;
}

Public IModelBinder GetBinder (ModelBinderProviderContext context)
{


Var mvcoptions=context. Services. GetService (a);


//need add a custom model class name
List M=new List (a);
M. dd (typeof (TestModel). Name);
M. dd (typeof (HouseModel). Name);
M. dd (typeof (HouseBuildingModel). Name);
M. dd (typeof (HouseRoomModel). Name);
M. dd (typeof (HouseSourceModel). Name);
M. dd (typeof (HouseTypeModel). Name);



String ModelName=context. Metadata. ModelType. Name;
If (the context==null)
Throw new ArgumentNullException (nameof (context));
if (! M.C ontains (ModelName))
return null;


if (! The context. The Metadata. IsComplexType & amp; & The context. The Metadata. ModelType==typeof (string))
{
//a simple type
Var loggerFactory=(ILoggerFactory) context. Services. GetService (typeof (ILoggerFactory));
Return new SimpleStringTrimModelBinder (context. Metadata. ModelType);
}
Else if (context) BindingInfo) holding!=null & amp; &
Context. BindingInfo. Holding. CanAcceptDataFrom (holding. Body))
{
//by/FromBody binding
Return new BodyStringTrimModelBinder (_formatters, context. Services. GetRequiredService ());
}

return null;
}
}
}
  •  Tags:  
  • C#
  • Related