hi, if i wanted to add common functionality to all pages i create a BasePage and drive all ASPX pages from it, how can you do that to Pageviews and Controllers in MVC ?
thanks in advanced.
hi, if i wanted to add common functionality to all pages i create a BasePage and drive all ASPX pages from it, how can you do that to Pageviews and Controllers in MVC ?
thanks in advanced.
Well for a controller, you just inherit from System.Web.Mvc.Controller
and add custom functionality:
public class MyController : System.Web.Mvc.Controller
{
public MyController() : base()
{
ViewData["someDataInMasterPage"] = "Hello World!";
}
}
If you'd have a Master Page which would try to retrieve some ViewData with the key above, it would make it accessible in all your views if their controller would inherit from MyController.
You could inherit from ViewPage to define custom functionality as well. See for example this SO question: http://stackoverflow.com/questions/610890/extending-webformview-in-mvc
For the Views ("Pageviews" as you call them) you probably won't have too much data / behaviour to group in a base class but you still can. You would define a class that inherits from System.Web.UI.Page and place all the common logic there. Then in your code behind files for the views (in ASP.NET MVC 1.0 there is no code behind file BY DEFAULT for the view but you can create it) inherit the view class from this common class.
For the controller you can create a class that inehrits from System.Web.Mvc.Controller and put all the common things in it. Then derive all your controller classes from this class. There's no restrictions on namings, namespaces and file location.
Hi, if you want all views to share the same script use a master page that has all the script functionality in it and put a place holder in it:
<asp:ContentPlaceHolder ID="MainContent" runat="server" />
And then each sub view then references the master page.
<%@ Page Title="" Language="C#" MasterPageFile="Site.Master"
Inherits="OpenProjects.Web.Mvc.ApplicationView %>
<asp:Content ID="Content1" ContentPlaceHolderID="MainContent" runat="server">
</asp:Content>
For the controllers, create you're own application controller that inherits from the built MVC controller and then get your controllers to inherit from that instead of just Controller