Monday, January 17, 2011 strongly typed view and virtual path provider

Use Case

Have custom system inspired by VirtualPathProvider samples from MSDN. Need ability to use strongly typed views.


In the case of using generic views (not-strongly types), everything works fine. The problem appears when I'm trying to use strongly-typed view as shown below:

<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage<Abc.Xyz.MyClass>" %>

The problem is obvious and well-known, if Views/ folder miss web.config file, mvc application can't load the view. In Virtual Path Provider solution is more tricky. The quickest solution (for me) was to add content of web.config file that must reside in views folder directly to main web.config file in separate location.

For example, if you have your VPP accepting smth like "~/page/xxx", the problem is easily solved by adding following snippet to your web.config


  <!-- Important change for virtual pages -->
  <location path="page">
        <add path="*" verb="*"

        Enabling request validation in view pages would cause validation to occur
        after the input has already been processed by the controller. By default
        MVC performs request validation before a controller processes the input.
        To change this behavior apply the ValidateInputAttribute to a
        controller or action.
          pageParserFilterType="System.Web.Mvc.ViewTypeParserFilter, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
          pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35"
          userControlBaseType="System.Web.Mvc.ViewUserControl, System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35">
          <add assembly="System.Web.Mvc, Version=, Culture=neutral, PublicKeyToken=31BF3856AD364E35" namespace="System.Web.Mvc" tagPrefix="mvc" />

      <validation validateIntegratedModeConfiguration="false"/>
        <remove name="BlockViewHandler"/>
        <add name="BlockViewHandler" path="*" verb="*" preCondition="integratedMode" type="System.Web.HttpNotFoundHandler"/>


During research, I've found several good articles:

No comments:

Post a Comment