Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement a Custom MVC Base View Page

I am attempting to implement an MVC custom base view page in order to "override" the User property type. This will make my CustomPrincipal type accessible in any view.

After searching the web, I found Phil Haack's instructions for implementing a custom base view page. I followed the instructions exactly as they are written, but I ran into an issue while accessing the properties in the view.

When I open a view, any previous Html helper actions are underlined with the blue, squiggly line. When I put my cursor over the @Html portion, it reveals the error:

"Html is ambiguous, imported from the namespaces or types 'System.Web.WebPages, System.Web.Mvc'."

Now, I understand why I am receiving the message, but I don't understand how to fix it. I don't know why this would matter, but the current application was created in Visual Basic. As a secondary test, I created another MVC application in C# and attempted to implement the custom base view page. In the C# application it worked just fine. I can access my custom property from within the views.

I have scoured the web looking for an answer to this issue, but so far have not found anything. Has anybody else run into a similar issue?

For reference, I included my custom base view page and ~/Views/web.config below:

BaseViewPage

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web.Mvc;
using CCInfoController;

namespace CCInfo.Web.Mvc
{
    public class BaseViewPage<TModel> : WebViewPage<TModel>
    {
        public new CustomPrincipal User
        {
            get
            {
                return base.User as CustomPrincipal;
            }
        }

        public override void Execute()
        {
        }
    }
}

~/Views/web.config

<configuration>
  <configSections>
    <sectionGroup name="system.web.webPages.razor"
      type="System.Web.WebPages.Razor.Configuration.RazorWebSectionGroup,
      System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
      PublicKeyToken=31BF3856AD364E35">
    <section name="host" 
       type="System.Web.WebPages.Razor.Configuration.HostSection,
       System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
       PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    <section name="pages"
       type="System.Web.WebPages.Razor.Configuration.RazorPagesSection,
       System.Web.WebPages.Razor, Version=1.0.0.0, Culture=neutral,
       PublicKeyToken=31BF3856AD364E35" requirePermission="false" />
    </sectionGroup>
  </configSections>

  <system.web.webPages.razor>
    <host factoryType="System.Web.Mvc.MvcWebRazorHostFactory,
       System.Web.Mvc, Version=3.0.0.0, Culture=neutral,
       PublicKeyToken=31BF3856AD364E35" />
    <pages pageBaseType="CCInfo.Web.Mvc.BaseViewPage">
      <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="System.Web.WebPages"/>
        <add namespace="CCInfoController" />
      </namespaces>
    </pages>
  </system.web.webPages.razor>
  ...
</configuration>
like image 247
Ryan V Avatar asked Jun 19 '13 15:06

Ryan V


1 Answers

You need to provide 2 versions of the WebViewPage, a generic and non-generic.

public class BaseViewPage<TModel> : WebViewPage<TModel>
{
}


public class BaseViewPage : WebViewPage
{
}
like image 199
shekky Avatar answered Sep 28 '22 05:09

shekky