Parser Error for Strongly Typed Master Page in MVC

2019-09-01 20:41发布

问题:

I am trying to create a STRONGLY TYPED Master Page (MVC 2.0) and getting following error:

Parser Error Message: Could not load type 'System.Web.Mvc.ViewMasterPage<Resorts.Services.ViewModels.BaseView>'.

Here is my code inside Master Page:

<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Resorts.Services.ViewModels.BaseView>" %>
<%@ Import Namespace="Resorts.Services.ViewModels" %>

Resorts.Services.ViewModels.BaseView is inside a seperate assembly and its referenced in Master Page. Resorts.Services.ViewModels.BaseView is NOT abstract class.

I saw a similar question was asked and resolved here BUT I couldn't figure out the solution:

Parse Error ViewMasterPage<TModel>

Here is ~Views\Web.Config file which I am not using in any way. Not sure if I need to make any changes inside this. If I delete this file, my Views are throwing parsing errors.

<?xml version="1.0"?>
<configuration>
  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>
    <pages
        validateRequest="false"
        pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
        userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
      <controls>
        <add assembly="System.Web.Mvc, Version=2.0.0.0, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />
      </controls>
    </pages>
  </system.web>
  <system.webServer>
    <validation validateIntegratedModeConfiguration="false" />
    <handlers>
      <remove name="BlockViewHandler"/>
      <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler" />
    </handlers>
  </system.webServer>
</configuration>

If I do <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage" %> in Master everything works fine BUT it doesn't like <%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<T>" %> :(

回答1:

Is the assembly where Resorts.Services.ViewModels.BaseView is in, also referenced in the MVC project itself?

Also your Import statement comes after the usage. Try

<%@ Import Namespace="Resorts.Services.ViewModels" %>
<%@ Master Language="C#" Inherits="System.Web.Mvc.ViewMasterPage<Resorts.Services.ViewModels.BaseView>" %>


回答2:

In your web.config try adding the assembly to the <assemblies> section after ensuring that the assembly is actually referenced by your project:

<compilation debug="true">
    <assemblies>
        ...
        <add assembly="Resorts.Services.ViewModels, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />
    </assemblies>
</compilation>

as well as the namespace to the namespaces section:

<pages>
    <namespaces>
        ...
        <add namespace="Resorts.Services.ViewModels"/>
    </namespaces>
</pages>


回答3:

Make sure the web.config file in the Views directory is good-to-go. Was missing mine and had this exact error.