2012-07-18 138 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對象的具體情況。

相關問題