2009-08-14 22 views
1

我正在爲使用ASP.NET MVC 1.0/C#的客戶端構建一個幫助臺票務系統。我已經實施了史蒂文桑德森的「App Areas in ASP.NET MVC, Take 2」,它工作得很好。手動ASP.net MVC區域和創建一個帶ID(SEO /乾淨URL)的ActionLink

public static void RegisterRoutes(RouteCollection routes) 
{ 
    // Routing config for the HelpDesk area 
    routes.CreateArea("HelpDesk", "ProjectName.Areas.HelpDesk.Controllers", 
     routes.MapRoute(null, "HelpDesk/{controller}/{action}", new { controller = "Ticket", action = "Index" }), 
     routes.MapRoute(null, "HelpDesk/Ticket/Details/{TicketId}", new { controller = "Ticket", action = "Details", TicketId = "TicketId" }) 
    ); 
}

所以,如果我在瀏覽器地址欄中輸入「http://localhost/HelpDesk/Ticket/Details/12」,我得到我預期的結果:

在我Globabl.asax頁我定義爲這樣一些途徑。這裏是我的控制器:

public ActionResult Details(int TicketId) 
{ 
    hd_Ticket ticket = ticketRepository.GetTicket(TicketId); 
    if (ticket == null) 
     return View("NotFound"); 
    else 
     return View(ticket); 
}

在我看來,我有:

<%= Html.ActionLink(item.Subject, "Details", new { item.TicketId })%> 

但是,代碼生成「http://localhost/HelpDesk/Ticket/Details?TicketId=12」,這也返回預期的結果。我的問題是...

如何在使用Steven Sanderson的Areas時定義ActionLink,它將創建一個乾淨的URL,如「http://localhost/HelpDesk/Ticket/Details/12」?

回答

4

嘗試

<%= Html.ActionLink(item.Subject, "Details", new { TicketId = item.TicketId })%> 

的ActionLink的方法需要相匹配的參數名稱是鍵的字典。 (請注意,傳遞一個匿名對象對此很方便)。其他任何我相信它只會標記到URL的末尾。

編輯:,這是不是爲你工作的原因是因爲你的第一個路由匹配,並優先(控制器和動作),但沒有定義TicketId參數。您需要切換路線的順序。你應該總是先把你最具體的路線。

+0

Womp ...有它!你是對的。這是命令! – robnardo 2009-08-17 12:48:21

1

嘗試

<%= Html.ActionLink(item.Subject, "Details", new { TicketId=item.TicketId })%> 
1

我認爲Womp有它...

哦,當你在你的交換路由嘗試

routes.MapRoute(null, "HelpDesk/Ticket/Details/{TicketId}", new { controller = "Ticket", action = "Details"}) 

我認爲,TicketId = "id"是搞亂東西

希望幫助,

+0

感謝丹,那也工作得很好 – robnardo 2009-08-17 12:48:59