如何将参数传递到MVC控制器中?

发布于 2025-01-30 12:40:45 字数 246 浏览 3 评论 0原文

将参数传递到ASP.NET核心MVC控制器的正确方法是什么?例如,我有一个具有以下签名的控制器:

[HttpPost]
public async Task<ActionResult<Input>> PostInput(Input input, string OutputPath)

注意:输入只是我完成的某些类。如何从我的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:

[HttpPost]
public async Task<ActionResult<Input>> PostInput(Input input, string OutputPath)

Note: Input is just some class I made. How do I call this function from my react application and pass in the parameters.

Thank you in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

熟人话多 2025-02-06 12:40:45

您可以尝试通过查询字符串传递outputpath,然后通过表单传递输入

public class Input {
        public int Id { get; set; }
        public string Name { get; set; }

    }

输入

[HttpPost]
        public async void PostInput(Input input, string OutputPath)
        {
            
        }


结果:


You can try to pass OutputPath via query string,and pass Input input via form.Here is a demo:

Input:

public class Input {
        public int Id { get; set; }
        public string Name { get; set; }

    }

action:

[HttpPost]
        public async void PostInput(Input input, string OutputPath)
        {
            
        }

request:
enter image description here
result:
enter image description here

enter image description here

Also,you can pass both OutputPath and Input input via query string:
enter image description here

海的爱人是光 2025-02-06 12:40:45

将参数传递到ASP.NET Core MVC控制器的正确方法是什么?例如,我有一个具有以下签名的控制器?

您不能在[来自Body] [from form from] 中都将两个参数一起传递在一起] 上下文。因此,要么您必须将输出路径移至类中,要么将类属性作为方法参数传递。您可以在在这里官方文件

有效的签名格式:1与方法参数时尚:

        [HttpGet]
        public async Task<ActionResult> PostInput(string input, string OutputPath)
        {
            return Ok();
        }

输出1:

”在此处输入图像描述“

有效的签名格式:2具有类时尚:

    [HttpPost]
    public async Task<ActionResult> PostInput(Input input)
    {
        return Ok();
    }

模型应为:

public class Input
    {
        public string InputPropertyName { get; set; }    
        public string OutputPath { get; set; }    
    }

输出2:

net/lmzf1.gif“ rel =“ nofollow noreferrer 还有任何其他问题,您可以看一下我们的

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:

enter image description here

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:

enter image description here

Note: if you still have any further concern you could have a look our
official document here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文