Hi,
I wanted to have more than one controller and view for same object/model in asp.net mvc, but it turned out to be a little complicated than I expected.
Basically what I want to do is something like this :
site.com/product -> will show product details to visitors. site.com/admin/product -> will show some extra information together with product details. and some commands like delete, edit etc.
The goal is to seperate the urls for the visitors and the admins.
My first approach was that I'd create a subfolder in the controller and the view folders, so the things would look this way :
> Controllers > Admin ProductController.cs ProductController.cs > Views > Admin > Product Index.aspx > Product Index.aspx
I can map the urls to the correct controllers without a problem (provided that I specify their namespaces, since the class names are the same). However, when I return the view from the Controller under admin folder, it does not display the Index view under the Views/Admin/Product folder but the one under Views/Product.
I know I can say return View("Full path here")
to return the view I want. But that's obviously not a good solution and I don't want to do that.
My question is, what is the best way to take care of this situation, with the condition of urls having to be that way?
Create a custom controller class that overrides the view method (or any other ones that needs to be overriden for this purpose) and makes sure the correct view is returned?
- This one is an ugly hack, but would work.
Create a custom view engine that also takes folders into consideration?
- I don't exactly know how I can do this, but as far as I understand, this should also solve the problem
Use Areas Prototype of Phil Haack?
- This one actually uses a different layout for folders, but in theory this should also work. But I am still not sure if this is the best way to do this, because having different Controller & View folders under the different parts of the site is not exactly what I want.
I'm leaning towards the custom view engine idea, that takes the folders into consideration when returns the views, but like I said, I don't know how to do that.
Any suggestions?
-
Try out Phil Haack's /Areas/ for your MVC project: http://haacked.com/archive/2008/11/04/areas-in-aspnetmvc.aspx
You'd have your normal Root for normal content, and a special /Areas/Admin/ section for all of your admin stuff (and the url will be /admin/).
0 comments:
Post a Comment