Sign In| Join
Get Help:Ask a Question in our Forums|Report a Bug|More Help Resources
Member
227 Points
61 Posts
Dec 26, 2007 09:47 PM|LINK
Dave Hayden won't the route of "admin/[controller]/[action]/[id]" by default go to the ProductsController as opposed to a ProductsAdminController
There's a ProductsAdmin controller defined like public class ProductsAdminController : AdminController { ... } so the URL is a little different:
admin/ProductsAdmin/delete/123
The regular ProductsController, in this case, does not have a Delete() action.
I guess that, if you want that to be automatic, you could use a custom RouteHandler for that route and alter the controller name on the fly:
public class AdminRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext context) { MvcHandler h = new MvcHandler(); string controller = context.RouteData.Values["controller"] as string; context.RouteData.Values["controller"] = controller + "Admin"; h.RequestContext = context; return h; } } //And change the route to: RouteTable.Routes.Add(new Route { Url = "admin/[controller]/[action]/[id]", Defaults = new { action = "Index", id = (string)null }, RouteHandler = typeof(AdminRouteHandler)/* <== CHANGED!!!*/ });
I haven't really tested the above code but it should be an interesting thing to try.
sergiopereir...
Member
227 Points
61 Posts
Re: How to Handle Admin Views and Controller Actions
Dec 26, 2007 09:47 PM|LINK
There's a ProductsAdmin controller defined like public class ProductsAdminController : AdminController { ... } so the URL is a little different:
admin/ProductsAdmin/delete/123
The regular ProductsController, in this case, does not have a Delete() action.
I guess that, if you want that to be automatic, you could use a custom RouteHandler for that route and alter the controller name on the fly:
public class AdminRouteHandler : IRouteHandler { public IHttpHandler GetHttpHandler(RequestContext context) { MvcHandler h = new MvcHandler(); string controller = context.RouteData.Values["controller"] as string; context.RouteData.Values["controller"] = controller + "Admin"; h.RequestContext = context; return h; } } //And change the route to: RouteTable.Routes.Add(new Route { Url = "admin/[controller]/[action]/[id]", Defaults = new { action = "Index", id = (string)null }, RouteHandler = typeof(AdminRouteHandler)/* <== CHANGED!!!*/ });Sergio Pereira
http://devlicio.us/blogs/sergio_pereira/