2

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

1 Answer 1

4

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.