【问题标题】:ASP.net c#, Same name method that takes different argument type [duplicate]ASP.net c#,采用不同参数类型的同名方法[重复]
【发布时间】:2012-07-17 21:28:23
【问题描述】:

可能重复:
Can you overload controller methods in ASP.Net MVC?

我需要 2 种采用不同类型参数的方法。所以我尝试了这个,

[HttpPost]
public ActionResult ItemUpdate(ORDER ln)
{
   // do something
}

[HttpPost]
public ActionResult ItemUpdate(List<ORDER> lns)
{
  // Do Something
}

但它不起作用。

编译时没有错误,但运行时出错。

我如何编写代码以使其正常工作?

谢谢!

[编辑]

[HttpGet]
public ActionResult ItemUpdate(string name)
{
    return Content(name);
}

[HttpGet]
public ActionResult ItemUpdate(int num)
{
    return Content(num.ToString());
}

当我调用 /Test/ItemUpdate/

出错了,

Server Error in '/' Application.
The current request for action 'ItemUpdate' on controller type 'OrderController' is ambiguous between the following action methods:
System.Web.Mvc.ActionResult ItemUpdate(System.String) on type Ecom.WebUI.Controllers.OrderController
System.Web.Mvc.ActionResult ItemUpdate(Int32) on type Ecom.WebUI.Controllers.OrderController 

[编辑]

即使是单个参数也不匹配ORDER。

if (lns.GetType() == typeof(ORDER))
{
  // always false
}else{
  // also I can not cast the object.
  ORDER ln = (ORDER)lns; //Unable to cast object of type 'System.Object' to type 'ORDER'
}

【问题讨论】:

  • 您遇到了什么错误?你想完成什么?
  • 我认为您走在正确的道路上。使用不同的签名、不同的参数类型重载 C# 方法是可以接受的。你的编译器能识别 ORDER 对象吗?是否因为方法不返回任何内容而犹豫不决(您暂时可以返回 null)?
  • 问题在于这是 MVC,请参阅:stackoverflow.com/questions/436866/…

标签: c# asp.net asp.net-mvc-3


【解决方案1】:

MVC 不支持重载操作。调度程序无法分辨这两个 Action 之间的区别。您可以通过为您的一项操作指定[HttpGet] 属性和另一项[HttpPost] 属性来解决此问题。

如果这不是一个选项(或者如果您有三个或更多重载),您始终可以自己调度 Action,方法是使用对象参数并使用运行时类型标识来选择要调用的正确函数。例如:

[HttpPost]
public ActionResult ItemUpdate(object arg)
{
    if (arg.GetType() == typeof(ORDER))
    {
        return ItemUpdateOrder((Order)arg);
    }
    else if (arg.GetType() == typeof(List<ORDER>))
    {
        return ItemUpdateList((List<Order>)arg);
    }
}

public ActionResult ItemUpdateOrder(ORDER ln)
{
    //...
}

public ActionResult ItemUpdateList(List<ORDER> lns)
{
    //...
}

【讨论】:

    【解决方案2】:

    你不能在控制器中这样做。您将不得不更改第二个方法名称。

    [HttpPost]
    public ActionResult ItemUpdates(List<ORDER> lns)
    {
      // Do Something
    }
    

    【讨论】:

    • 没有。在这种情况下,它不会是相同的View
    • -1 问题是:Is it possible to make SAME NAME method for....
    • @daniloquio - 澄清了答案。
    • @James 你的意思是不能为不同类型的参数创建同名方法吗?
    • @Expert 想要成为 - 在 c# 中很好,但在 MVC 控制器中不行。这就是它编译但在您尝试访问它时失败的原因。
    【解决方案3】:
    public ActionResult ItemUpdates(object myInputValue)
    {
        if (myInputValue.GetType() == typeof(string)
        // Do something
        else if (myInputValue.GetType() == typeof(List<ORDER>))
        // Do something else
    }
    

    然后您可以将对象转换为您选择的类型并正常操作。

    【讨论】:

      【解决方案4】:

      在 ASP.NET 中,不可能有没有ActionFilter 属性的重载方法来区分这些操作。这样做的原因是 ActionInvoker(在 Controller 基类中用于调用动作的类)无法确定调用哪个方法,因为它需要为每个“询问”一个 ModelBinder(负责构造动作参数对象)如果 ModelBinder 可以从传递的 HTTP 请求构造该参数对象,则重载。对于简单的场景,这会起作用,但在更复杂的场景中,这会失败,因为 ModelBinder 将成功绑定多个重载的参数。在 ASP.NET MVC 中不允许重载是非常聪明的设计决策。

      要解决您的问题,您可以修复ItemUpdate HTTP GET 操作,只需保留第二个操作并只执行一个操作,因为 ModelBinder 不介意作为 URL 参数传递的值是否为 @987654323 @ 或 int

      [HttpGet]
      public ActionResult ItemUpdate(string name)
      {
          return Content(name);
      }
      

      对于ItemUpdate HTTP POST 版本,我建议重命名这些操作之一或只有一个操作,列表版本,因为更新单个ORDER 只是更新多个ORDER 对象的特定情况.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2018-11-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2014-10-10
        • 1970-01-01
        • 2015-05-01
        相关资源
        最近更新 更多