Custom Base Class for template file.

Aug 13, 2011 at 11:31 PM

It would be lovely if I could add a custom base class instead of the generated base class.


Aug 14, 2011 at 8:21 AM

You should be able to using @inherits, e.g.

@inherits YourBaseClass


Aug 14, 2011 at 7:31 PM

In fact, we recently made changes to the template generator to not emit the base class if you specify one. (

Sep 3, 2011 at 8:46 PM

This doesn't appear to work, I've tried using the pageBaseType in my config and also using the @inherits directive to specify my base class, but the generator still spits out System.Web.Mvc.WebViewPage<TModel>

Sep 4, 2011 at 1:58 AM

If you get the sources, there is a sample that shows this working. Look under samples\TemplateSample\PreProcessedTemplate.cshtml. That might help you identify the difference with what you're doing.

Sep 4, 2011 at 7:15 AM

Hey David. I spotted the issue. The generator directive has to be the first directive on the page for this to work. I had a couple of using directives before it, moving the generator directive above them made it work.

Sep 4, 2011 at 7:17 AM

However, why can't I change the base type for MVC views?

Sep 4, 2011 at 7:20 AM

You can! :) I just verified that by adding '@inherits Foo' as the first line in an MVC view. The generated code then used it.

Sep 4, 2011 at 7:35 AM

Ah, but the difference is I'm using strongly typed view models so the razor generator errors with:

"Custom tool error: The 'inherits' keyword is not allowed when a 'model' keyword is used."

Sep 4, 2011 at 7:44 AM

That's not specific to RazorGenerator. Generally, the two attributes don't make sense together. If you want a strongly typed model but don't want to extend WebViewPage<Foo>, just specify the inherits. e.g. '@inherits YourCustomViewPage<Foo>', making @model unneeded.

Sep 4, 2011 at 7:56 AM

Yeah, sorry about that, I came into MVC in MVC3 when the @inherits directive just wasn't used any more in favour of the clearer @model directive and using pageBaseType in your web.config. Which leads me to another question, why isn't the pageBaseType attribute supported from the razor web.configs?

Sep 4, 2011 at 7:30 PM

It could conceivably work. It's a bit tricky because those are normally runtime settings, and we need to read then at design time, but it should be solvable. Feel free to open a bug to track this. Or even send a pull request! :)