gpt4 book ai didi

c# - 传递给 Controller ​​操作的参数

转载 作者:太空宇宙 更新时间:2023-11-03 19:27:33 26 4
gpt4 key购买 nike

我有一个带有回发操作的 Controller :

[HttpPost]
public ActionResult test(string x) { ... }

我想添加一个具有相同签名的 GET 操作:

public ActionResult test(string y) { ... }

但是编译器吐了:

Type 'TestController' already defines a member called 'test' with 
the same parameter types

所以我想更改签名:

public ActionResult test(RouteValueDictionary args) { ... }

并这样调用它:

@{
RouteValueDictionary args = new RouteValueDictionary(
new { y = "test" }
);
}
@Html.Action("test", "TestController", args)

但 Controller 接收到的 args 为 null。显然我不明白这应该如何工作。我知道我可以重命名该操作,但我想知道如何声明它以便我的字典出现。

TIA-e

最佳答案

您可以保留签名并只更改方法的名称,但通过指定 ActionNameAttribute 使用相同的操作名称。

例如

[HttpGet]
public ActionResult Foo(FooModel model)
{
// do stuff
}

[HttpPost]
[ActionName("Foo")]
public ActionResult FooPost(FooModel model)
{
// do stuff
}

方法名称不同,但两者的 MVC 操作相同:“Foo”。

关于c# - 传递给 Controller ​​操作的参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7466550/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com