views:

46

answers:

1

Hello, I'm building a web app (front-end and admin section). There are 4 main record types (books, cars, houses, deals). The user can do query and see the detail of each record type, the admin can do query as well and in addition can edit and delete.

Front end

  • controller: each record type has the following actions

//
// GET: /{recordType}/Index
//
// GET: /{recordType}/Search

  • routing:

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+" } );
[...]

  • views: there is a folder for each record types with two pages Index.aspx (search form and result grid) and Details.aspx.

Admin

  • controller: there is a main action the renders an empty view each record type has the following actions

[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

  • routing: I'm struggling because I would like to reflect my controller logic but I don't want to create a huge amount of routes.

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

  • views: there is an Admin folder with all the pages EditBook.aspx, SearchBook.aspx but actually I don't like this.

==

What do you think? How can I avoid to add a lot of routes for each record type?

Thanks!

Lorenzo.

A: 

You need to include the "Action" in your route definition. Potentially the following could provide all your needs in a singe route definition.

routes.MapRoute( "Route", "/Admin/{RecordType}/{Action}/{id}", new { controller = "Books", action = "Search", recordType = "Book", id = UrlParameter.Optional } );

Then you would need action methods such as:

public ActionResult Edit (RecordTypes? recordType, int id) {...} public ActionResult Save (RecordTypes? recordType, int id) {...} etc

Clicktricity