当前位置:网站首页>How to make the minimum API bind the array in the query string
How to make the minimum API bind the array in the query string
2022-07-27 06:53:00 【Dotnet cross platform】
Preface
In the last article , We did 《 Give Way ASP.NET Core Supports binding arrays in query strings 》:
[HttpGet]
public string Get([FromQuery][ModelBinder(BinderType = typeof(IntArrayModelBinder))] int[] values)
{
return string.Join(" ", values.Select(p => p.ToString()));
}however , The same implementation , At the minimum API But it reported an error :

that , Use minimum API Can you realize the same function ?
In depth exploration
Implement extension methods
Through the error prompt , We found the smallest API Looking for static of type TryParse Method :
No public static bool int[].TryParse(string, out int[]) method found for values.” therefore , We try to be int[] Type creation TryParse Extension method :
public static class IntArrayExtentions
{
public static bool TryParse(this int[] obj, string value, out int[] result)
{
result = value.Trim('[', ']').Split(',').Select(str => int.Parse(str)).ToArray();
return true;
}
}however , The test found no effect .
Through the call stack :

We found the search static TryParse Source code for method :
private MethodInfo? GetStaticMethodFromHierarchy(Type type, string name, Type[] parameterTypes, Func<MethodInfo, bool> validateReturnType)
{
bool IsMatch(MethodInfo? method) => method is not null && !method.IsAbstract && validateReturnType(method);
var methodInfo = type.GetMethod(name, BindingFlags.Public | BindingFlags.Static | BindingFlags.FlattenHierarchy, parameterTypes); and type.GetMethod Method cannot get extension method .
It seems that the road is blocked !
Custom type
Since the extension method doesn't work , That can only be used to define static TryParse Method's custom type :
public class DemoDto
{
public int[] Array { get; set; }
public static bool TryParse(string value, out DemoDto result)
{
result = new DemoDto { Array = value.Trim('[', ']').Split(',').Select(str => int.Parse(str)).ToArray() };
return true;
}
}
app.MapGet("/",
(DemoDto values) =>
{
return string.Join(" ", values.Array.Select(p => p.ToString()));
});Run again , Successful implementation :

Conclusion
For routing 、 Query and header binding source , Minimum API Static by type TryParse Method to bind custom types .
TryParse There are two API:
public static bool TryParse(string value, out T result);
public static bool TryParse(string value, IFormatProvider provider, out T result);Add microsignals 【MyIO666】, Invite you to join the technical exchange group
边栏推荐
- Detection and identification data set and yolov5 model of helmet reflective clothing
- deepsort源码解读(七)
- 关于ES6的新特性
- What "hard core innovations" does Intel have in the first half of 2022? Just look at this picture!
- Converting ArcGIS style stylesheet files to GeoServer SLD files
- pymysql查询查询结果转换json
- How can chrome quickly transfer a group of web pages (tabs) to another device (computer)
- MangoDB
- 2022年全球6家最具技术实力的的智能合约审计公司盘点
- FTP service introduction and configuration
猜你喜欢

【12】 Understand the circuit: from telegraph to gate circuit, how can we "send messages from thousands of miles"?

Linux安装与卸载MySql

Li Hongyi 2020 deep learning and human language processing dlhlp core resolution-p21

Disk management and file system

Express框架

According to SQL, you must know and learn SQL (MySQL)

Multimodal database | star ring technology multimode database argodb "one database for multiple purposes", building a high-performance Lake warehouse integrated platform

2022年全球6家最具技术实力的的智能合约审计公司盘点

如何删除或替换EasyPlayer流媒体播放器的loading样式?

Linux Installation and uninstallation of MySQL
随机推荐
ES6新特性(入门)
Three methods to judge whether it is palindrome structure
2022年全球6家最具技术实力的的智能合约审计公司盘点
Sunflower teaches you how to prevent denial of service attacks?
What is special about the rehabilitation orthopedic branch of 3D printing brand?
Shell script delete automatically clean up files that exceed the size
Common font and color settings of markdown documents
Go语言学习
Redis快速学习
LVM与磁盘配额
Linux安装与卸载MySql
Boostrap
关于过快S验证码的一些问题
where接自定义函数导致查询缓慢
3D打印品牌的康复骨科支具有何特别之处?
What is the reason why dragging the timeline is invalid when playing device videos on the easycvr platform?
Project training experience 2
deepsort源码解读(六)
Shell common commands - memos
Recommended by the world's most technologically competent smart contract security audit company in 2022