开发者

ASP.NET MVC generic base view class

开发者 https://www.devze.com 2022-12-19 10:22 出处:网络
I can have a base class for views in an MVC project like this: public class BaseViewPage : System.Web.Mvc.ViewPage

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 tha开发者_Go百科n 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.


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

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号