1

I'm a fairly experienced .NET programmer, as well as a MVC programmer with PHP. Now I'm new at MVC3 and trying to build my first work on it, so I'm dealing with a few questions. For starters, how do I extend the Controller Class? Can someone point me to a guide/list of methods I should implement?

Thanks!

2 Answers 2

4

You don't have to implement any methods to extend the controller, although obviously it would be rather silly not to. You just inherit from it, and override the methods you wish to alter.

If you don't know what methods to alter, I have to question why you want to extend it?

EDIT:

You may benefit from the two big MVC sample applications, Nerddiner and Music Store. They give you a very good idea of how to make ecommerce sites and the like in MVC. Don't take them as gospel, because they are samples and are intended to be simple. They don't currently make use of best practices like Dependency Injection, or Repository design. Some links to useful tutorials:

Lots of good videos here. The Pluralsight stuff is pretty straight forward

The Music Store tutorial app

NerdDinner tutorial

Sign up to request clarification or add additional context in comments.

18 Comments

With web forms I always had BasePage class extending System.Web.UI.Page, in which I wrote some properties, constants and virtual methods and overrided methods such as OnPreInit, to do something before the other Page classes were loaded. I'm trying to figure how to get such a behaviour from Controllers.
@nosuchnick - MVC is very different from Webforms. You don't generally override anything in the pipeline because you don't have all the stupid hooks for the control system. You pretty much do all you work in an Action method, and do all your formatting in the view. Things like constants are probably better off in static utility classes.
Ok, I understand that. But even in PHP, with MVC framework codeigniter, i use a base controller to handle cross site basic stuff, like logged user.
@nosuchnick - Logged in user is already handled by the framework, if you're using Memebership (which you should be). I'm not saying there is no need to override a base class, but it's a lot less frequent than most people used to need it. You can always extend the controller class, and use your extension. But it's very straight forward. You only have to add whatever you want to add, nothing special.
@gauravvgat - I did not just point to the MVC tutorials. I pointed to them in addition to answering the question. The question was what methods had to be implemented to extend the controller, and the answer, as I correctly answered.. is none.
|
1

You don't need to implement anything, just make your class inherit from System.Web.Mvc.Controller. Normally there is no real benefit to doing this, but in some cases it can be helpful make some form of common custom base controller class that all controllers in your project could share.

Beware though, when adding common methods to your controllers. It often makes more sense to add these methods to some lower tier of your application, or as helpers methods on your models or viewmodels.

3 Comments

Thanks... please read my comment to previous answer in which I explain myself a little better.
Yeah, well, I stand by my answer. In my experience there is very little benefit to using a custom base controller. For authentication and logging users and whatnot, you could make custom actionfilters though. Controllers should really just be actionhandlers, and they should be as slim as possible. See this presentation: viddler.com/explore/mvcconf/videos/1 (it's for MVC2 but all the points are still valid for MVC3. Despite the lousy recording, it is really good)
Thank you. I'm new to this, so I'm sure I'll learn something.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.