這個名字很可能會讓人困惑。我有一個要求URL必須是名稱友好的以表示日期(schedule/today
,schedule/tomorrow
等)。我不想搞亂我的路線映射與DateTime.Now
,DateTime.Now.AddDays(1)
等不同的參數,所以我決定創建一個映射到同一個名稱的行動路線:動作呈現第一個叫做Action的視圖(當一個動作叫另一個動作時)
routes.MapRoute(RouteNames.ScheduleToday, "schedule/today", new { controller = "Schedule", action = "Today" });
routes.MapRoute(RouteNames.ScheduleTomorrow, "schedule/tomorrow", new { controller = "Schedule", action = "Tomorrow" });
採取的行動的想法是,我希望能夠調用Today()
操作,但實際上調用List(DateTime date)
操作,例如DateTime.Now
作爲date
參數。
這是這樣的偉大工程:
public ActionResult Today()
{
return this.List(DateTime.Now);
}
public ViewResult List(DateTime date)
{
this.ViewData["Date"] = date;
return this.View("List");
}
我希望能夠調用this.View()
而不是this.View("List")
。這可能不是我上面發佈的內容嗎?看起來好像呈現的視圖與第一個操作的名稱相匹配,因爲獲取此操作的唯一方法是明確呈現List
視圖。
是的,謝謝你的想法,但我認爲它真的太過複雜了,需要做什麼。調用'this.View(「List」)'就足夠了。 – TheCloudlessSky 2010-09-09 01:08:27