views:

390

answers:

1

Do I need a separate controller for the below?

http://localhost/bookmarks --> bookmarks controller http://localhost/bookmark/{bookmarkid} --> bookmark controller

Is there any way I can combine them both into one controller? I want to keep the uri's the same i.e. bookmark singular indicating fetch a single bookmark.

Thanks

+4  A: 

Assuming a C# development environment

Global.asax:

routes.MapRoute(
  "Bookmarks",
  "bookmarks",
  new { controller = "Bookmarks", action = "Bookmarks"}
);

routes.MapRoute(
  "Bookmark",
  "bookmark/{bookmarkid}",
  new { controller = "Bookmarks", action = "Bookmark" }
);

BookmarksController:

public ActionResult Bookmarks()
{
   //Get all bookmarks, I presume. :)
   return View();
}

public ActionResult Bookmark(string bookmarkid)
{
   //Do stuff with the bookmark id
   return View();
}

Using this routing scheme, http://localhost/bookmarks will hit the Bookmarks action in the Bookmarks controller, and http://localhost/bookmark/123456 will hit the Bookmark action in the Bookmarks controller.

Dan Atkinson
fantastic thanks. Didn't know you could do that.
NabilS
You're very welcome. :)
Dan Atkinson