简体   繁体   English

错误已使用相同的参数类型定义了一个名为“索引”的成员

[英]Error already defines a member called 'Index' with the same parameter types

In my Controller i have the following 2 methods; 在我的控制器中,我有以下两种方法:

[ActionName("index")]
public ActionResult Index()
{
    return View();
}

and

 public ActionResult Index()
 {
    var m =MyMod();

    return View(m);

 }

Even though i used [ActionName("index")] i get an error saying that Error 1 Type 'MyProject.Controllers.MyController' already defines a member called 'Index' with the same parameter types 即使我使用[ActionName("index")]我仍然收到一条错误消息,指出Error 1 Type 'MyProject.Controllers.MyController' already defines a member called 'Index' with the same parameter types

How can i prevent this ? 我该如何预防呢?

No, this is not possible, you cannot have 2 actions with the same name on the same controller using the same HTTP verb. 不,这是不可能的,您不能在同一控制器上使用相同的HTTP动词进行两个具有相同名称的操作。 Also from C#'s perspective you cannot have 2 methods on the same class with the same name and same parameters. 同样从C#的角度来看,同一类上不能有2个方法具有相同的名称和参数。 The compiler won't let you do that. 编译器不允许您这样做。

You could make one of the 2 actions be accessible with a different HTTP verb. 您可以使用不同的HTTP动词使这2个动作之一可访问。 This is usually the convention when you have 2 actions with the same name. 当您有两个名称相同的动作时,通常这是惯例。 The first is used to render a view and the second is decorated with the [HttpPost] attribute and used to process the form submit from the view. 第一个用于呈现视图,第二个用[HttpPost]属性修饰并用于处理从视图提交的表单。 The post action also takes a view model as parameter containing the form submission fields. 发布操作还采用视图模型作为包含表单提交字段的参数。 So the 2 methods have different signatures and it will make the compiler happy. 因此,这两种方法具有不同的签名,这将使编译器满意。 Here's the recommended approach: 这是推荐的方法:

public ActionResult Index()
{     
    MyViewModel model = ...
    return View(model);
}

[HttpPost]
public ActionResult Index(MyViewModel model)
{
    ...
} 

From the compiler's point of view, those two methods are the same. 从编译器的角度来看,这两种方法是相同的。 They have the same name, return type and parameters (in this case none). 它们具有相同的名称,返回类型和参数(在本例中为无)。 That is why you are getting the error. 这就是为什么您得到错误。

Did you mean to create an overload for Index taking a parameter ? 您是要为使用参数的Index创建重载吗?

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误 - 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Error - already defines a member called 'InitializeComponent' with the same parameter types 类型'Startup'已经定义了一个名为'Configuration'的成员,它具有相同的参数类型 - Type 'Startup' already defines a member called 'Configuration' with the same parameter types 已经使用相同的参数类型定义了一个名为“InitializeComponent”的成员 - Already defines a member called 'InitializeComponent' with the same parameter types Web Api已经使用相同的参数类型定义了一个名为“ Get”的成员 - Web Api already defines a member called 'Get' with the same parameter types 已经定义了一个使用相同参数类型调用的成员 c# - already defines a member called with the same parameter types c# “'Form1' 已经定义了一个名为 '.ctor' 的成员,具有相同的参数类型” - "'Form1' already defines a member called '.ctor' with the same parameter types " C#错误:类型'x'已经定义了一个名为'y'的成员,它具有相同的参数类型 - C# error: Type 'x' already defines a member called 'y' with the same parameter types 错误1类型“ Pay”已经使用相同的参数类型定义了一个名为“ ComputePay”的成员 - Error 1 Type 'Pay' already defines a member called 'ComputePay' with the same parameter types 我在尝试插入公共类时出错,“PlayerCollision”已经定义了一个名为“OnCollisionEnter”的成员,其参数类型相同 - I have a error trying to insert a public class , 'PlayerCollision' already defines a member called 'OnCollisionEnter' with the same parameter types 我该如何修复错误:Type Form1'已经定义了一个名为'Dispose'的成员,它具有相同的参数类型? - How can i fix the error: Type Form1' already defines a member called 'Dispose' with the same parameter types?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM