Is it possible to create a Partial class for the controller in MVC?
Reason : I have a controller which contains too many code. This controller contains the code for different reports and due to too many code it reduces the readability of the code. I want to keep controller name same for all reports so I want to distribute code for the different reports.
If i can do any other way than do let me know.
Thanks
Alok Shah
Yes you can use a partial class – the compiler just merges them all up a compile-time to produce one.
However, you mention you want to ‘distribute’ code – if the code is to go into multiple projects then partials are not going to help.
You can instead create base controller classes (that inherit from
System.Web.Mvc.Controllerin a class library project, with the necessary action methods, and then simply use routing to route similar urls to different controllers. Here’s a link to the Asp.Net Tutorial ‘Creating Custom Routes’ which might be useful for that. I hightly recommend looking through the rest of the tutorial if there are other core aspects of MVC you’re not sure about. It doesn’t cover everything, but it does the basics.A web application that needs to use those then simply has its own controllers inherit from those redistributable ones. Even better, you can now create extensibility points through virtual methods etc if there are elements of these reports which different web apps might need to customise.
So
"Reports/Customers"could route to aCustomerReportsControllerand"Reports/Orders"could route to anOrdersReportsController:This is the most simplistic approach to this problem – but I think using Areas (link is to MSDN walkthrough) would most likely also be applicable, in which case you might not need to use routing at all because the default route would probably do it all.