我能做到的方式與此類似,它不會引發錯誤:MVC控制器動作超載
public class AdsController : Controller
{
AdsRepository repo = new AdsRepository();
public ActionResult Details(string id)
{
AdSlots adslot = new AdSlots();
Enum.TryParse(id, true, out adslot);
return Content(repo.GetInvocationCode(adslot, Global.SiteInfo.ID));
}
[ChildActionOnly]
public ActionResult Details(AdSlots slot)
{
return Content(repo.GetInvocationCode(slot, Global.SiteInfo.ID));
}
}
的一點是,我想有通過訪問資源的方式/廣告/細節/使用排行榜在瀏覽器的URL,而且在母版頁:
<% Html.RenderAction("Details", "Ads", new {slot = AdSlots.Leaderboard }); %>
我只是覺得比清潔:
<% Html.RenderAction("Details", "Ads", new {slot = "Leaderboard" }); %>
如果我做如上並調用URL /廣告/信息/排行榜我得到這個錯誤:
The current request for action 'Details' on controller type 'AdsController' is ambiguous between the following action methods.. etc
我沒想到我會得到這個錯誤,因爲我已經給出了屬性[ChildActionOnly]到其他行動。我認爲那對系統來說顯而易見的是,如果我通過瀏覽器訪問它,我只會有一個選項,不是那麼明顯。