Currently I have a folder structure like this:
Area (folder)
- Toolkit (folder)
- Controllers (folder)
- AdminController.cs
- Views (folder)
- Admin (folder)
- Privledges (folder)
- Create.cshtml
- Edit.cshtml
- Delete.cshtml
Which translates to
/Toolkit/{controller}/{action}/{tool}/{id}
Is it a bad practice to set up the action to behave a like a controller that serves up a view based on the string {tool} parameter and parameter {id} passed to the action?
The implementation of what I am talking about:
private const string FOLDER_PRIVILEGES = "./Privileges/";
public ActionResult Privileges(string tool, string id = "")
{
dynamic viewModel = null;
ToolViews view; // enum for the views
// Parse the tool name to get the enum representation of the view requested
bool isParsed = Enum.TryParse(tool, out view);
if (!isParsed)
{
return HttpNotFound();
}
switch (view)
{
case ToolViews.Index:
viewModel = GetIndexViewModel(); // call a function that gets the VM
break;
case ToolViews.Edit:
viewModel = GetEditViewModelById(int.Parse(id)); // sloppy parse
break;
default:
viewModel = GetIndexViewModel();
break;
}
// The folder path is needed to reach the correct view, is this bad?
// Should I just create a more specific controller even though it would
// require making about 15-20 controllers?
return View(FOLDER_PRIVILEGES + tool, viewModel);
}
When I write a View, I have to make sure the Path name is used for the folder
@Html.ActionLink("Edit", "./Toolkit/Admin/Priveleges/Edit", "Admin", new { id = item.id })
This seems to be a poor practice, because if the folder structure changes at all it will require a lot of maintenance.
However, if I have to break out the actions into controllers there would be many of them (almost 20 with more added over time).
If what I am doing is a bad practice, what would be the best way to serve a route that looks like this?
/Toolkit/Admin/Privileges/Edit/1
I want to avoid doing the following:
/Toolkit/Admin/CreatePrivileges/1
/Toolkit/Admin/EditPrivileges/1
/Toolkit/Admin/DeletePrivileges/1
Please let me know if I’m not making any sense, because I am having a hard time putting this question into words.
I think you’re trying to force a convention into MVC that goes against its orignal intent.
With MVC, your controller is a noun, and your action is a verb. Using your examples, you have:
So as you can see, if you can consider Toolkit + Admin as area + subarea, or combine them into one area (TookitAdmin), it would get you back to the original purpose for controllers and actions.
Based on the comments, it sounds like you may have decided to go this way. But I wanted to point out that the conclusion you came to in a round-about way is getting back to the roots of MVC.
As a side note, have you considered moving to MVC4? Its Web API provides better support for a RESTful API, which it sounds like you may be trying to get to.