简体   繁体   English

ASP.NET MVC网址路由

[英]ASP.NET MVC Url Routing

Do I need a separate controller for the below? 我需要以下单独的控制器吗?

http://localhost/bookmarks --> bookmarks controller http://localhost/bookmark/ {bookmarkid} --> bookmark controller http:// localhost / bookmarks- >书签控制器http:// localhost / bookmark / {bookmarkid}->书签控制器

Is there any way I can combine them both into one controller? 我有什么办法可以将它们组合成一个控制器? I want to keep the uri's the same ie bookmark singular indicating fetch a single bookmark. 我想保持uri的相同,即书签单数表示获取单个书签。

Thanks 谢谢

Assuming a C# development environment 假设一个C#开发环境

Global.asax: Global.asax中:

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

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

BookmarksController: 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. 使用此路由方案, http:// localhost / bookmarks将在Bookmarks控制器中单击Bookmarks操作,而http:// localhost / bookmark / 123456将在Bookmarks控制器中单击Bookmark操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM