2012-07-18 128 views
3

可能重复:
Can you overload controller methods in ASP.Net MVC?ASP.net C#,同名方法,它有不同的参数类型

我需要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()); 
} 

,当我打电话/测试/ 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' 
} 
+1

你得到了什么错误?你想达到什么目的? – 2012-07-18 18:49:22

+0

我认为你走在正确的道路上。可以使用不同的签名来重载C#方法,比如不同的参数类型。你的编译器是否能识别ORDER对象?它是否因为方法不返回任何内容而出现问题(您可以暂时返回null)? – DOK 2012-07-18 18:52:33

+3

问题是,这是MVC,请参阅:http://stackoverflow.com/questions/436866/can-you-overload-controller-methods-in-asp-net-mvc – CaffGeek 2012-07-18 18:53:51

回答

1

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

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

号在这种情况下,它不会是相同的'查看' – 2012-07-18 18:54:48

+0

-1问题是:'是否有可能为...创建SAME NAME方法。 – daniloquio 2012-07-18 19:14:34

+0

@daniloquio - 澄清了答案。 – James 2012-07-18 19:19:01

2

MVC中不支持重载操作。调度员无法分辨两个操作之间的差异。您可以通过给予您的某个动作[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) 
{ 
    //... 
} 
0
public ActionResult ItemUpdates(object myInputValue) 
{ 
    if (myInputValue.GetType() == typeof(string) 
    // Do something 
    else if (myInputValue.GetType() == typeof(List<ORDER>)) 
    // Do something else 
} 

你可以再投对象,以你的类型的选择和正常操作。

0

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

要解决您的问题,您可以修复ItemUpdate HTTP GET操作,只需将第二个操作离开并仅执行一个操作即可,因为ModelBinder不介意以URL参数形式传递的值是否为stringint

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

对于ItemUpdate HTTP POST版本我建议你重新命名这些动作中的一个或只有一个动作,该列表的版本,因为更新单个ORDER仅更新多个ORDER对象的具体情况。

相关问题