Code
·
72 lines
·
2104 bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72# Mvc.ModelBinding.MultiParameter
Nuget package https://www.nuget.org/packages/Mvc.ModelBinding.MultiParameter/
Calling controller methods using multiple unique parameters.
```c#
[HttpPost("~/api/SomeMethod/{SomeParameter2}")]
public IActionResult SomeMethod(
string Referer, // "https://localhost:44346/"
string SomeParameter2, // "two"
string SomeParameter3, // "three"
ApiModel SomeParameter4, // {four}
double SomeParameter5, // 5.5 (multi binding FromBody)
int SomeParameter6) // 6 (multi binding FromQuery)
{
return Ok();
}
```
Using attributes to emphasize the source:
```c#
[HttpPost]
[Route("~/api/OtherMethod/{SomeParameter2}")]
public IActionResult OtherMethod(
[FromCooky(Name = ".AspNetCore.Session")] string SomeParameter0, // #######
[FromHeader(Name = "Referer")] string SomeParameter1, // "https://localhost:44346/"
[FromRoute] string SomeParameter2, // "two"
[FromQuery] string SomeParameter3, // "three"
[FromBody] ApiModel SomeParameter4, // {four}
[FromBody] double SomeParameter5, // 5.5 (multi binding FromBody)
[FromQuery] int SomeParameter6) // 6 (multi binding FromQuery)
{
return Ok();
}
```
This test uses `netproxy` javascript caller for posting Json to controllers.
```javascript
r = await netproxyasync("./api/SomeMethod/two?SomeParameter3=three&SomeParameter6=6",
{
"SomeParameter4": // Now the beast has a name
{
Name: "four",
"Users":
[
[{ Name: "User00", Alias: ['aliasa', 'aliasb', 'aliasc'] }, { Name: "User01" }],
[{ Name: "User10" }, { Name: "User11" }],
[{ Name: "User20" }, { Name: "User21" }]
]
},
"SomeParameter5": 5.5 // double binder
});
```
For the multi parameter binding use the `WithMultiParameterModelBinding`.
Example Program.cs:
```c#
var builder = WebApplication.CreateBuilder(new WebApplicationOptions
{
ContentRootPath = AppDomain.CurrentDomain.BaseDirectory
});
builder.Services.AddMvcCore().WithMultiParameterModelBinding();
var app = builder.Build();
app.UseRouting();
app.MapControllers();
app.UseDefaultFiles();
app.UseStaticFiles();
app.Run();
```