简体   繁体   中英

How do I limit the actions in my controller in an ASP.NET MVC application?

I writing an MVC app and I'm really struggling to keep my controllers lean and limit the number of actions.

For example, here is a look at my ReportController actions: OpenCall
ClosedCall
ServiceLevelAgreement
Barrier
Resolution
Repair
Failure
Inventory
CustomerLocation

These are all my different reports. Should I be making a controller for each one?

Here is a look at my ServiceCallController actions: New
Create
Reopen
UpdateETA
UpdateOnsite
UpdateServiceTimes
UpdateEnroute
Close
Cancel
Reassign
Show
ModifyAfterClose

This are all different actions I need to take based on what the user wants to do. Can anyone help me out here with how to clean this up?

I think it's clean already. If an object of yours is capable of doing 7-8 actions, then your structure is the way to go. If you had one controller that is responsible for the actions of multiple business objects, then I'd have suggested you change it into the way your current design is in.

One thing though about your current design, it'd be a good idea to just have one update action on ServiceCallController and that action could take what to update as a parameter. After all, all those update actions do one thing : update your business object.

But if you are still not satisfied, then you can just have one action that's named "do" which takes the action as parameter and passes that to a service layer which returns what the request wanted. But IMO, that wouldn't be a good design, so I definitely don't suggest it.

如果要使用的文件太大,则可以使用部分类,也可以将逻辑退回到一个单独的类,然后让控制器进行思考和倾斜。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM