传递给控制器​​动作的参数

ekk*_*kis 1 c# razor asp.net-mvc-3

我有一个带回发动作的控制器:

[HttpPost]
public ActionResult test(string x) { ... }
Run Code Online (Sandbox Code Playgroud)

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

public ActionResult test(string y) { ... }
Run Code Online (Sandbox Code Playgroud)

但编译器呕吐:

Type 'TestController' already defines a member called 'test' with 
the same parameter types
Run Code Online (Sandbox Code Playgroud)

所以我想改变签名:

public ActionResult test(RouteValueDictionary args) { ... }
Run Code Online (Sandbox Code Playgroud)

并称之为:

@{
    RouteValueDictionary args = new RouteValueDictionary(
        new { y = "test" }
    );
}
@Html.Action("test", "TestController", args)
Run Code Online (Sandbox Code Playgroud)

但控制器收到argsnull.显然我不明白这是怎么回事.我知道我可以重命名该动作,但我想知道如何声明它,以便我的字典遇到.

TIA - e

Jay*_*Jay 6

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

例如

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

[HttpPost]
[ActionName("Foo")]
public ActionResult FooPost(FooModel model)
{
   // do stuff
}
Run Code Online (Sandbox Code Playgroud)

方法名称不同,但两者的MVC操作相同:"Foo".

  • 如果它们是完全相同的签名,我更喜欢在帖子模型中添加一个`FormCollection`参数,这样我就不必担心将来可能会改变的魔术字符串:) (2认同)