As one request is used to get one web page, could we call to these controllers "page controllers"?
Why would you? They're named controllers. After all, an action on a controller doesn't neccessarily serve a "web page", it might respond using JSON data, XML, RSS or only a part of a web page (a 'control' if you want).
If a widget represent a classic asp.net webcontrol or usercontrol, and i want to render this widget in a lot of pages, how could avoid repeat the logic of the widget if this logic is in the "page controller"?
- Make sure your (business) logic is not in the controller: it doesn't belong there. The controller's job is to map input/output, not to handle logic.
- The views that contain your widget should have a
ViewModel
which contains the ViewModels needed in the widgets, e.g.
-
MainViewModel
{
UserWidgetViewModel UserViewModel;
List<Foo> Bar;
// ...
}
UserWidgetViewModel
{
string UserName;
int Reputation;
//...
}
I felt this unelegant first, but I've been using this for some time now and I believe it nicely encapsulates the necessary data.
Hope that helps