我试图使用BindAsync手动绑定数据,我遵循了Microsoft的文档,这里有一个链接:Microsoft docs.但这对我不起作用.
我有以下简单的代码
var app = builder.Build();
app.MapGet("/Test", ([FromQuery] Student student) =>
{
return $"Student: {student.Id}, {student.Name}";
}).WithTags("Student");
这是班上的学生
public class Student
{
public int Id { get; set; }
public string Name { get; set; }
public static ValueTask<Student> BindAsync(HttpContext context, ParameterInfo parameter)
{
string name = context.Request.Query["name"];
int.TryParse(context.Request.Query["id"], out var id);
var result = new Student
{
Name = name,
Id = id
};
return ValueTask.FromResult<Student>(result);
}
}
我有两个问题
- 我收到一个错误,未找到"public static bool TryParse"
- 即使我像这样在学生课堂上添加简单的TryParse
public static bool TryParse(string value, out Student student)
{
student = new();
return true;
}
我仍然收到来自端点"Message": "Required parameter "Student student" was not provided from query string."的错误消息,即使我使用swagger发送了它,并且URL是这样的"https://localhost:5001/student?id=7&name=myName"