Page view counter

Typed ViewPage Using Generics Without Code Behind

Last post 10-22-2008 5:33 PM by koistya. 15 replies.

Sort Posts:

  • Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 2:08 AM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 9:36 AM
    • Missoula, MT
    • Posts 57
    • Points 205

    I've successfully created .aspx view pages without code behind by replacing the first line with this:

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>

    However, trying to change it to inherit the generics to get typed view data like the following doesn't seem to work:

     <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage<MvcApplication.Models.MyModel>" %>

    Is there some trick I'm missing here? Can you not use a generic class in the Inherits attribute?

  • Re: Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 5:02 AM
    Answer
    • Loading...
    • JeremyS
    • Joined on 10-21-2006, 8:23 AM
    • UK
    • Posts 96
    • Points 480

    You need to use the standard CLR notation for generics. For example, I have an Error Handling View which deals with processing exceptions. The view inherits from ViewPage<Exception> so this is what you'd have in the view: 

    <%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage`1[[System.Exception, mscorlib]]" %>

     
     Hope this is useful.

  • Re: Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 12:55 PM
    • Loading...
    • ghotiman
    • Joined on 08-28-2002, 9:36 AM
    • Missoula, MT
    • Posts 57
    • Points 205

    Thanks!  Where did you find this?  It doesn't say anything about it in the online help for the page directive.  I thought that the `1 was a typo, but it's not...

     After a little more searching I found this: https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104071&wa=wsignin1.0

  • Re: Typed ViewPage Using Generics Without Code Behind

    12-13-2007, 1:08 PM
    • Loading...
    • JeremyS
    • Joined on 10-21-2006, 8:23 AM
    • UK
    • Posts 96
    • Points 480
    I'm glad it works for you :) I didn't actually find any information about this anywhere - I just ran into it when doing some debugging.
  • Re: Typed ViewPage Using Generics Without Code Behind

    10-21-2008, 2:45 PM
    • Loading...
    • koistya
    • Joined on 03-03-2006, 11:36 PM
    • St. Petersburg [rus]
    • Posts 42
    • Points 136

    Looks like removing code-behind file and decalring inheriting class on .aspx/.ascx page itself doesn't bring any value. Because intellisense doesn't work in this scenario.

    Let's vote for this bug:

    https://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=104071

    Maybe VS dev team will fix it.

    My LinkedIn Profile - ASP.NET C# Ninja | My News Feeds
  • Re: Typed ViewPage Using Generics Without Code Behind

    10-21-2008, 8:20 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 685
    • Points 3,665
    • AspNetTeam

    koistya:
    Looks like removing code-behind file and decalring inheriting class on .aspx/.ascx page itself doesn't bring any value. Because intellisense doesn't work in this scenario.

    I'm not sure what is meant by that statement.  Intellisense works just fine as long as the Inherits attribute is set correctly.  The easiest way I've found to do this is to use a typeof() to get a Type corresponding to the ViewPage<> you want, then to look at its Name property.  Copy + paste this string into your .aspx file.  For example:

    Type viewPageType = typeof(ViewPage<IList<Class1>>);
    string mangledName = viewPageType.FullName;

    In my application, this produces a mangled name of System.Web.Mvc.ViewPage`1[[System.Collections.Generic.IList`1[[MvcApplication5.Models.Class1, MvcApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]].  So the @ Page directive would look like:

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage`1[[System.Collections.Generic.IList`1[[MvcApplication5.Models.Class1, MvcApplication5, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]], mscorlib, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]]" %>

    This horrendous-looking name is how the CLR identifies types.  It is independent of any language used (VB, C#, C++, dynamic languages, etc.).  Since the directives are not language-specific (note that the Language attribute is just a string attribute, but it doesn't change how the directives are parsed), a language-neutral type representation had to be chosen.  This is the CLR's language-neutral representation of generics.

  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 6:07 AM
    • Loading...
    • koistya
    • Joined on 03-03-2006, 11:36 PM
    • St. Petersburg [rus]
    • Posts 42
    • Points 136

    Here is an example:

    ViewsWithoutCodeBehind.zip

    Intellisense doesn't work without code-behind files and inheriting generic class declared on .aspx page in CLR way.

    ViewsWithoutCodeBehind

    My LinkedIn Profile - ASP.NET C# Ninja | My News Feeds
  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 2:35 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 685
    • Points 3,665
    • AspNetTeam

    Intellisense works just fine; it's just that your @ Page directive is incorrect.  It should read (note the Inherits attribute):

    <%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" AutoEventWireup="true" Inherits="System.Web.Mvc.ViewPage`1[[ViewsWithoutCodeBehind.Models.HomeModel, WebSite, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]" %>

  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 3:00 PM
    • Loading...
    • koistya
    • Joined on 03-03-2006, 11:36 PM
    • St. Petersburg [rus]
    • Posts 42
    • Points 136

    You just have explicitly added Version, Culture and PublicKeyToken to my example though all they are optional as far as I know. Anyway I have also added them and have same issues with Intellisense:

    ViewsWithoutCodeBehind2.png

    My LinkedIn Profile - ASP.NET C# Ninja | My News Feeds
  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 3:02 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 685
    • Points 3,665
    • AspNetTeam

    koistya:
    You just have explicitly added Version, Culture and PublicKeyToken to my example though all they are optional as far as I know.

    No, I did not.  Your actual type name was incorrect.  Use the typeof(ViewData<>).FullName trick I mentioned earlier in this thread and you will produce the actual correct mangled name for the type.

  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 4:32 PM
    • Loading...
    • koistya
    • Joined on 03-03-2006, 11:36 PM
    • St. Petersburg [rus]
    • Posts 42
    • Points 136

    I have double-checked the declaration. Type name is correct. Maybe it doesn't work when ReSharper is installed... Website buils without problems, displayed correctly in browser, but Intellisense still doesn't work one .aspx/.ascx view pages without code-behind files.

    My LinkedIn Profile - ASP.NET C# Ninja | My News Feeds
  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 4:43 PM
    • Loading...
    • JeremyS
    • Joined on 10-21-2006, 8:23 AM
    • UK
    • Posts 96
    • Points 480

    koistya:
    Maybe it doesn't work when ReSharper is installed...
     

    You're correct - ReSharper doesn't support the CLR notation for generics. 

  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 5:10 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 685
    • Points 3,665
    • AspNetTeam

    Look very closely at your Inherits attribute.  You're looking for a type called ViewsWithoutCodeBehind.Views.Home.Index<...>, but your sample project contains no such type.  The behavior you likely wanted is that your view inherits directly from the System.Web.Mvc.ViewPage<...> type, which is what the sample @ Page directive I provided to you does.

  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 5:12 PM
    • Loading...
    • koistya
    • Joined on 03-03-2006, 11:36 PM
    • St. Petersburg [rus]
    • Posts 42
    • Points 136

    No, look at my last (second) screenshot, i am using System.Web.Mvc.ViewPage<...>

    My LinkedIn Profile - ASP.NET C# Ninja | My News Feeds
  • Re: Typed ViewPage Using Generics Without Code Behind

    10-22-2008, 5:16 PM
    • Loading...
    • levib
    • Joined on 07-23-2007, 7:50 PM
    • Redmond, WA
    • Posts 685
    • Points 3,665
    • AspNetTeam

    Ah, my browser must have cached the original image before you updated it.  My apologies.

Page 1 of 2 (16 items) 1 2 Next >