views:

497

answers:

3

Hey Stackers!

I am building a Help Desk Ticket system for a client using ASP.NET MVC 1.0 / C#. I have implemented Steven Sanderson's "App Areas in ASP.NET MVC, Take 2" and it is working great.

In my Globabl.asax page I have some routes defined as such:

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" })
    );
}

So, if I enter "http://localhost/HelpDesk/Ticket/Details/12" in the browser address bar manually, I get the results I expect. Here is my controller:

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

In my view I have:

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

But that code generates "http://localhost/HelpDesk/Ticket/Details?TicketId=12" which also returns the expected results. My Question is...

How do I define an ActionLink when using Steven Sanderson's Areas that will create a clean URL like: "http://localhost/HelpDesk/Ticket/Details/12" ?

+1  A: 

Try

<%= Html.ActionLink(item.Subject, "Details", new { TicketId=item.TicketId } )%>
John Sheehan
+4  A: 

Try

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

The ActionLink method expects a dictionary with keys that match the parameter names. (Note that passing an anonymous object is a convenience for this). Anything else I believe it will just tag onto the end of the URL.

EDIT: The reason that this isn't working for you is because your first route matches and takes precedence (controller and action), but defines no TicketId parameter. You need to switch the order of your routes. You should always put your most specific routes first.

womp
Womp... there it is! You are correct. It was the order!
robnardo
+1  A: 

I think Womp has it ...

Oh and while you are swapping your routes try

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

I think the , TicketId = "id" is messing things up

Hope that helps,

Dan

Daniel Elliott
thanks Dan, that worked as well
robnardo