ASP.net c#,采用不同参数类型的同名方法 [英] ASP.net c#, Same name method that takes different argument type
本文介绍了ASP.net c#,采用不同参数类型的同名方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:
可以在ASP.Net MVC中重载控制器方法吗?
我需要2个采用不同类型参数的方法.所以我尝试了这个
I need to 2 methods that takes different type of argument. so I tried this,
[HttpPost]
public ActionResult ItemUpdate(ORDER ln)
{
// do something
}
[HttpPost]
public ActionResult ItemUpdate(List<ORDER> lns)
{
// Do Something
}
但它不起作用.
编译时没有错误,但是运行时会出错.
No error while compiling, but when run, it makes an error.
我如何编写代码以使其正常工作?
How I write the code to make that works?
谢谢!
[HttpGet]
public ActionResult ItemUpdate(string name)
{
return Content(name);
}
[HttpGet]
public ActionResult ItemUpdate(int num)
{
return Content(num.ToString());
}
当我致电/Test/ItemUpdate/
and when I call /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匹配.
It does not match with ORDER even single parameter.
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'
}
推荐答案
您无法在控制器中执行此操作.您将不得不更改第二个方法名称.
You can't do that in a controller. You will have to change the second method name.
[HttpPost]
public ActionResult ItemUpdates(List<ORDER> lns)
{
// Do Something
}
这篇关于ASP.net c#,采用不同参数类型的同名方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文