2010-04-12 30 views
20

我有這個路由集合:路由到有相同的名字,但不同參數的操作

 routes.MapRoute(
      "IssueType", 
      "issue/{type}", 
      new { controller = "Issue", action = "Index" } 
     ); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{id}", // URL with parameters 
      new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults 
     ); 

這裏是控制器類:

public class IssueController : Controller 
{ 
    public ActionResult Index() 
    { 
     // todo: redirect to concrete type 
     return View(); 
    } 

    public ActionResult Index(string type) 
    { 
     return View(); 
    } 
} 

爲什麼,當我要求http://host/issue我得到The current request for action 'Index' on controller type 'IssueController' is ambiguous between the following action methods:
我希望當沒有參數時,第一種方法應該起作用,而當指定某個參數時,第二種方法應該起作用。

我犯了什麼錯誤?

UPD:可能重複:Can you overload controller methods in ASP.NET MVC?

UPD 2:由於上面的鏈接 - 沒有做出行動超載任何合法的方式,是嗎?

UPD 3:動作方法不能被基於參數重載(c)中http://msdn.microsoft.com/en-us/library/system.web.mvc.controller%28VS.100%29.aspx

回答

10

我將具有尋找一個有效的類型變量一個索引方法

public class IssueController : Controller 
{ 
    public ActionResult Index(string type) 
    { 
     if(string.isNullOrEmpty(type)){ 
      return View("viewWithOutType");} 
     else{ 
      return View("viewWithType");} 
    } 
} 

編輯:

如何創建一個自定義屬性來尋找特定的請求值,如在這篇文章中StackOverflow

[RequireRequestValue("someInt")] 
public ActionResult MyMethod(int someInt) { /* ... */ } 

[RequireRequestValue("someString")] 
public ActionResult MyMethod(string someString) { /* ... */ } 

public class RequireRequestValueAttribute : ActionMethodSelectorAttribute { 
    public RequireRequestValueAttribute(string valueName) { 
     ValueName = valueName; 
    } 
    public override bool IsValidForRequest(ControllerContext controllerContext, MethodInfo methodInfo) { 
     return (controllerContext.HttpContext.Request[ValueName] != null); 
    } 
    public string ValueName { get; private set; } 
} 
+0

是的,很明顯,但對這一問題 – zerkms 2010-04-13 03:14:59

+1

是的沒有實際的解決方案,這帶來了一些有些超載但太髒,所以我選擇重新命名第二種方法。謝謝 – zerkms 2010-04-13 05:17:23

1

你可以使用一個ActionFilterAttribute來檢查參數(我試過),但這是一個壞主意。 每個不同的操作應該有自己的名字。

爲什麼不直接調用你的兩個方法「索引」和「單個」,並說出命名的限制?

與基於匹配簽名在編譯時綁定的方法不同,末尾的缺失路由值被視爲空值。

如果你想讓[hack] ActionFilterAttribute匹配參數讓我知道,我會發佈一個鏈接,但正如我所說,這是一個壞主意。

+0

我已經重命名第二,所以我有2種方法。而且我得到了很多ActionFilterAttribute的鏈接,的確 - 這是一個糟糕的解決方案。不管怎樣,謝謝你。 – zerkms 2010-04-13 05:16:18

1

您只需用[HttpPost]標記您的第二個動作即可。例如:

public class IssueController : Controller 
{ 
    public ActionResult Index() 
    { 
     // todo: redirect to concrete type 
     return View(); 
    } 

    [HttpPost] 
    public ActionResult Index(string type) 
    { 
     return View(); 
    } 
} 
+7

誰告訴你第二個郵寄地址? – zerkms 2011-08-27 23:31:57

+0

對我來說似乎很奇怪... – 2015-03-16 16:09:35

+0

我經常看到這種情況,特別是對於登錄方法 - 一個獲取操作以提供初始視圖,另一個獲取操作以接受登錄請求並重定向。通常我也看到了第一個''[HttpGet]'標籤,不確定哪個(如果有的話)框架版本需要這個標籤,但是標記這兩個標籤使得它更容易推理。 – brichins 2018-01-12 17:57:55

5

我遇到類似的情況,我想我的「指數」的行動來處理渲染,如果我有一個ID指定與否。我遇到的解決方案是將Index參數的ID參數設置爲可選。 例如,我最初試圖同時具有:

public ViewResult Index() 
{ 
    //... 
} 
// AND 
public ViewResult Index(int entryId) 
{ 
    //... 
} 

,我只是將它們結合在一起,並把它改爲:

public ViewResult Index(int entryId = 0) 
{ 
    //... 
} 
+0

這可能是.NET 4.0的解決方案,但我使用的是3.5那些日子;-) – zerkms 2011-09-02 22:29:12

相關問題