tags:

views:

87

answers:

2

In my MVC apps I normally declare a base view type that all of my views inherit from. I get a parser error when I specify Inherits="MyView" in my Page declaration, but not if I specify Inherits="MyApp.Web.Views.MyView".

Strangely enough, it works fine if I specify a strongly typed view name: Inherits="MyView<T> (where T is any valid type).

Why can I specify a strongly typed view without the full type name, but not a generic view?

My base view class is declared like this:

namespace MyApp.Web.Views {
    public class MyView : MyView<object> {
    }

    public class MyView<TModel> : ViewPage<TModel> where TModel : class {
    }
}

UPDATE: Note that I do import MyApp.Web.Views via web.config. If I did not, then the strongly typed approach (Inherits="MyView<T>") wouldn't work either.

+1  A: 

You can add your namespace to the <namespaces> element of your web.config file then you should be able to use Inherits="MyView"

<pages>
  <namespaces>
    <add namespace="System.Web.Mvc" />
    <add namespace="System.Web.Mvc.Ajax" />
    <add namespace="System.Web.Mvc.Html" />
    <add namespace="System.Web.Routing" />
    <add namespace="MyApp.Web.Views" />
  </namespaces>
</pages>
David G
I _do_ import the namespace like this. Otherwise the strongly typed view [Inherits="MyView[ModelType]"] wouldn't work either.
Seth Petry-Johnson
@Seth: But, have you added the namespace in the web.config?
Eduardo Molteni
A: 

From another post: Here's the underlying issue: the ASP.NET page parser does not support generics as a page type

Read more here- http://stackoverflow.com/questions/1480373/generic-inhertied-viewpage-and-new-property

moomi
Very interesting... I haven't read all of the code on that page yet, but it sounds like the issue I've reported is caused by some of the generic/non-generic juju that's being done behind the scenes. If I'm successful at using Justin's code to resolve my issue I'll post an update here.
Seth Petry-Johnson