But at the same time with me to take over the front end, put forward the professional requirements,
Output should have unified format,
Want to have a swagger
But before the project is to use the.net Framework written Framework, not pure single webapi,
Helpless, I took him to upgrade to the latest. Net6 framework, basic can cope with the demand of the front end, but found a serious problem, before receiving parameters is very free, automatic binding is very high, such as defining the parameters of type int, front the null values, can also automatically restore to default value 0, but rises to. Net6 adopts the model of webapi, ineffective, redefined as int? Engineering is too big, to find the Internet a lot of data and Microsoft's official documents, add this BBS's enthusiastic help and answer, finally solved for me, wrote a custom rebind the data modeling methods, there is a hidden danger, but in the process of rebound, must know in advance the type of parameter itself, so I use swith, illustrates all types of my project for the reduction, and int [] of this type, haven't solve, I don't know how to restore, if we can remove front-end for null values of parameters, and use the method of automatically binding binding template form is perfect, but always can't find relevant syntax online, do not know to have a way to master, thank you,
using the BLL;
Using Microsoft. AspNetCore. Mvc. ModelBinding;
Using the Model.
Using Newtonsoft. Json;
Using Newtonsoft. Json. Linq;
Using System;
Using System. Collections. Generic;
Using System. IO;
Using System. The Runtime. Serialization. Json;
Using System. The Text;
Using System. The Threading. The Tasks;
The namespace ZFY_API. Filter
{
///& lt; Summary>
///this function is the parameters of the filter value is null
///& lt;/summary>
Public class UserModelBinder: IModelBinder
{
Public Task BindModelAsync (ModelBindingContext bindingContext)
{
JObject j=new JObject ();
//get the interface parameter type
Type Type=bindingContext. ModelMetadata. ModelType;
//define an interface type parameter
Var Data=https://bbs.csdn.net/topics/Activator.CreateInstance (Type);
//gain parameters, in the form of jobject save standby
The object C=new object ();
String bodyStr=string. The Empty;
Using (var reader=new StreamReader (bindingContext. HttpContext. Request. Body, Encoding, UTF8, true, 1024, true))
{
Var bodyRead=reader. ReadToEndAsync ();
J=JsonConvert DeserializeObject(bodyRead. Result);
}
JObject jParam=new JObject ();
The foreach (var rs in j)
{
If (rs. A null Value=https://bbs.csdn.net/topics/=)//exclude the transfer of null reference
{
JParam. Add (new JProperty (rs. Key, rs. Value));
}
}
//manually rebind data model
String obStr=jParam. ToString ();
C=JsonConvert. DeserializeObject
To solve the problem is how to this line of code GetObjectFromJson
GetObjectFromJson
GetObjectFromJson
GetObjectFromJson
. .
Type, etc., like the simple, known well, but if
Especially the type of the custom, this is unknowable
This method doesn't work, so if to solve the deficiency of the mentioned, directly in the Startup injection is more simple
CodePudding user response:
Is I think too much, actually don't need to swith, direct the object class lineInfo. SetValue (Data, GetObjectFromJson