Asp.net-mvc – ASP.NET MVC generic base view class

asp.netasp.net-mvcviews

I can have a base class for views in an MVC project like this:

public class BaseViewPage : System.Web.Mvc.ViewPage
{
  public string Something { get; set; }
}

And then in the ASPX I can do this:

<%@ Page Something="foo" Language="C#" Inherits="MyNamespace.BaseViewPage" %>

This works fine; the problem is when I try to do the same with the generic version:

public class BaseViewPage<TModel> : 
    System.Web.Mvc.ViewPage<TModel> where TModel : class
{
  public string Something { get; set; }
}

When I try to use this from the ASPX, like this:

<%@ Page Something="foo" Language="C#" 
    Inherits="MyNamespace.BaseViewPage<SomeClass>" %>

I get the error message:

Error parsing attribute 'something': Type 'System.Web.Mvc.ViewPage' does not have a public property named 'something'.

Notice how it tries to use System.Web.Mvc.ViewPage rather than my BaseViewPage class. To make it more interesting, if I remove the "Something" attribute from the Page directive and put some code in the generic version of BaseViewPage (OnInit for example) the class is actually called / used / instantiated.

So, am I doing something wrong or is this a limitation / bug? Thanks.

Best Answer

I had this exact same question a few months ago. Drove me nuts until Justin Grant answered it here.