如何将参数传递给 MVC 控制器?
How to pass parameters into MVC controllers?
将参数传递到 Asp.Net 核心 MVC 控制器的正确方法是什么。例如,我有一个具有以下签名的控制器:
[HttpPost]
public async Task<ActionResult<Input>> PostInput(Input input, string OutputPath)
注意:输入只是一些 class 我做的。如何从我的 React 应用程序调用此函数并传入参数。
提前致谢!
"What is the proper way to pass parameters into Asp.Net core MVC controllers. For example, I have a controller with the following signature?"
You cannot pass two parameter together either in [FromBody]
neither in
[FromForm]
because its not allowed to pass more than one parameter
within one action specially for [FromBody]
context . So either you
have to move the OutputPath into the class or pass the class property
as method argument. You could get more details on the official document here
.
Valid Signature Format: 1 With Method Argument fashion
[HttpGet]
public async Task<ActionResult> PostInput(string input, string OutputPath)
{
return Ok();
}
Output: 1
Valid Signature Format: 2 With Class Fashion
[HttpPost]
public async Task<ActionResult> PostInput(Input input)
{
return Ok();
}
Model Should Be:
public class Input
{
public string InputPropertyName { get; set; }
public string OutputPath { get; set; }
}
Output: 2
Note:
If you still have any further concern you could have a look our
official document here
.
希望以上步骤能相应地指导您。
您可以尝试通过查询字符串传递 OutputPath
,通过 form.Here 传递 Input input
是一个演示:
输入:
public class Input {
public int Id { get; set; }
public string Name { get; set; }
}
动作:
[HttpPost]
public async void PostInput(Input input, string OutputPath)
{
}
要求:
结果:
此外,您可以通过查询字符串同时传递 OutputPath
和 Input input
:
将参数传递到 Asp.Net 核心 MVC 控制器的正确方法是什么。例如,我有一个具有以下签名的控制器:
[HttpPost]
public async Task<ActionResult<Input>> PostInput(Input input, string OutputPath)
注意:输入只是一些 class 我做的。如何从我的 React 应用程序调用此函数并传入参数。
提前致谢!
"What is the proper way to pass parameters into Asp.Net core MVC controllers. For example, I have a controller with the following signature?"
You cannot pass two parameter together either in
[FromBody]
neither in[FromForm]
because its not allowed to pass more than one parameter within one action specially for[FromBody]
context . So either you have to move the OutputPath into the class or pass the class property as method argument. You could get more details on theofficial document here
.
Valid Signature Format: 1 With Method Argument fashion
[HttpGet]
public async Task<ActionResult> PostInput(string input, string OutputPath)
{
return Ok();
}
Output: 1
Valid Signature Format: 2 With Class Fashion
[HttpPost]
public async Task<ActionResult> PostInput(Input input)
{
return Ok();
}
Model Should Be:
public class Input
{
public string InputPropertyName { get; set; }
public string OutputPath { get; set; }
}
Output: 2
Note:
If you still have any further concern you could have a look our
official document here
.
希望以上步骤能相应地指导您。
您可以尝试通过查询字符串传递 OutputPath
,通过 form.Here 传递 Input input
是一个演示:
输入:
public class Input {
public int Id { get; set; }
public string Name { get; set; }
}
动作:
[HttpPost]
public async void PostInput(Input input, string OutputPath)
{
}
要求:
此外,您可以通过查询字符串同时传递 OutputPath
和 Input input
: