我正在构建一个Web应用程序(前端和管理部分)。有4种主要记录类型(书籍,汽车,房屋,交易)。用户可以查询并查看每种记录类型的详细信息,管理员也可以进行查询,另外还可以编辑和删除。
前端
//
// GET: /{recordType}/Index
//
// GET: /{recordType}/Search
routes.MapRoute("BookDetail", "Books/{Id}", new { controller = "Books", action = "Details", id = Optional }, new { Id = @"\d+" } );
routes.MapRoute("CarDetail", "Cars/{Id}", new { controller = "Cars", action = "Details", id = Optional }, new { Id = @"\d+" } );
[...]
管理的
[HttpGet]
public virtual ActionResult SearchRecord(RecordTypes? recordType){return View(GetViewNameFromRecordType(recordType));}
//
// GET: /Admin/{recordType}/Create
//
// POST: /Admin/{recordType}/Create
//
// GET: /Admin/{recordType}/Search
//
// POST: /Admin/{recordType}/Search
//
// GET: /Admin/{recordType}/Edit/1
//
// POST: /Admin/{recordType}/Save/1
//
// GET: /Admin/{recordType}/Delete/1
//
// POST: /Admin/{recordType}/Delete/1
routes.MapRoute("BookDetail", "Admin/Books/Save/{Id}", new { controller = "Books", action = "SaveBook", recordtype = "Book" } );
routes.MapRoute("BookDetail", "Admin/Books/Create/{Id}", new { controller = "Books", action = "CreateBook", recordtype = "Book" } );
[...]
==
你怎么看?如何避免为每种记录类型添加大量路由?谢谢!
洛伦佐。
答案 0 :(得分:0)
您需要在路线定义中包含“操作”。可能以下内容可以在单一路线定义中提供您的所有需求。
routes.MapRoute( “路线”, “/管理/ {记录类型} / {行动} / {ID}”, new {controller =“Books”,action =“Search”,recordType =“Book”,id = UrlParameter.Optional} );
然后你需要采取行动方法,例如:
public ActionResult Edit(RecordTypes?recordType,int id){...} public ActionResult Save(RecordTypes?recordType,int id){...} 等