views:

69

answers:

3
+1  Q: 

Base page in MVC 2

Hi,

I have just moved over to using ASP.NET MVC 2. In web forms, I normally had a BasePage class that extends from System.Web.UI.Page. And then every page extends from this BasePage. In this BasePage class I have methods that I need. How would I do this in an MVC application?

Any samples would be appreciated.

Thanks.

+3  A: 

It is a bit different in MVC. The equivallent would be BaseController although this doesn't correlate exactly to a page in the classic ASP.NET sense. For a start, a controleler doesn't have any markup.

Into a base controller you might inject any model classes that are required by all pages and any common behaviours that have to be executed as part of all Action requests. An example might be some custom checks to go into the Controller OnActionExecuting event...

http://msdn.microsoft.com/en-us/library/system.web.mvc.controller.onactionexecuting.aspx

    protected override void OnActionExecuting(ActionExecutingContext filterContext)
    {
        //check the filterContext for a certain condition
        if (condition) {
            //do something else - redirect to a different route or 
            //render a different view to to the default
            filterContext.Result = new RedirectResult(newUrl); 
        }
        //Otherwise, do nothing, the requested Action will execute as normal...
    }
Daniel Dyson
+1  A: 

We also moved from base Page classes in ASP.NET and found that a combination of a base controller and a base Model (ViewData) class works well.

So ex Page properties eg: CurrentUser are available from the base Controller and also passed to the base ViewData when its initiated so you can use them on the aspx page.

Mark Redman
+2  A: 

IN MVC there is a greater separation of concerns for rendering the UI, so depending on what the code did in your base page will dictate where it goes in MVC.

If your code generated HTML than you will probably be creating custom HTML helpers and reusable partials views (.ascx). If it handled input data than it will go in a model binder class, and you can create a base model binder for common code. If it talked to your services and domain model than it will go in the controller, and again you can use a base controller. Queries to the persistence layer will go in your model, and reusing code here leads to a much larger discussion of your architecture.

Jace Rhea
In my BasePage I had a method called GetUser. It would get the current employee number of the user using the system. Where would this method go to now?
Brendan Vogt
@bioluminescence In general the controller will be making the request to get the user from your model (domain/business objects and services), which in turn will be backed by other layers of abstraction that get your user from the persistence layer. So the answer is...it depends. Does your code actually go to the database and get the user directly or did it call a service that was backed by other layers of abstraction to service the request?
Jace Rhea