Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I break up a controller in ASP.NET MVC while preserving the controller name?

I'm working on a controller that is a part of a very flat hierarchy of pages, and it looks like one section of the site could have over a dozen action methods, not including their corresponding post actions. Is there a good way to break this controller up while still preserving the section name? I don't want that controller class getting out of control.

like image 935
Brandon Linton Avatar asked Feb 28 '23 13:02

Brandon Linton


2 Answers

If your controller class is getting out of control on a dozen actions, you might want to rethink how much logic should be in there and how much can be refactored out into services.

Also, if your controller is getting up to much more than a dozen actions, you might want to consider whether it can be broken down into separate controllers. Remember, you can change the routing rules and controller factory such that the URL remains the same but the controller is broken up.

If you're comfortable that you've done what you can and still want to break it into separate files - use a partial class.

like image 96
pdr Avatar answered Apr 06 '23 08:04

pdr


If I understand correctly, you just want to split a class across multiple files so each file is easier to understand.

That would mean you want to use Partial Classes. They allow you to split the definition of a single class across multiple files.

like image 35
Justin Niessner Avatar answered Apr 06 '23 07:04

Justin Niessner