In general, how many actions per controller? RRS feed

  • Question

  • User422934000 posted

    I'm developing an application that's becoming sizable, in terms of number of views that a given user might interact with.

    I started with a handful of controllers, one controller for each major area of functionality for the application (think one for users, called /Users, and one for administrators, called /Admin).  Each of these has now grown to where it starts to feel unwieldy -- the /Users controller has about 50 actions now.

    Is there a way to build sub-controllers, so that the routes look nice, like /Users/Settings, so that I can still group related actions into controllers but split them up into logical subgroups?

    Ideally, I think, the directory structure and routes would reflect something like:

    /Controllers/Users.cs, /Controllers/Users/UserSettings.cs, /Views/Users/Index.cshtml, /Views/Users/Settings/Index.cshtml, and so on.

    My other thought was to split them up (UsersController.cs, UsersSettingsController.cs, etc) and then manually specify routes to methods to maintain a nice, logical grouping of routes, but that seems clumsy and easily broken.

    Tuesday, April 9, 2019 1:42 PM

All replies

  • User-474980206 posted

    see MVC areas for breaking the project into a user area and an admin area.

    Tuesday, April 9, 2019 1:51 PM
  • User422934000 posted

    Hi Bruce,

    Thanks, that looks like it's exactly what I was hoping for!

    Now, I'm running into another problem.

    I've moved some code into an area, but for some reason, when I POST data to an action, it's always coming in empty.

    The POST action looks like this:

    public IActionResult SettingsPost(GeneralSettingsViewModel vm)
        return Redirect("/Area/Settings");

    But no matter what, the "vm" object always comes in with all values null.

    When this wasn't in an area, but just in the original controller, it worked fine.

    Is there something else I'd need to do to get POST methods to work inside areas?

    Tuesday, April 9, 2019 3:20 PM
  • User1724605321 posted

    Hi stevehav ,

    You may provide the codes about posting data , via ajax or form post ? 

    Best Regards,

    Nan Yu

    Thursday, April 11, 2019 1:48 AM