1

所以,我个人认为这是一种打击。

我将 .aspx 模板放在非标准位置。在这个例子中,它有一个虚拟路径~/Content/Sites/magical/Index.aspx

然后我创建了自己的视图引擎作为测试,它扩展了 WebFormsViewEngine:


public class MagicalWebFormsViewEngine : WebFormViewEngine
{
    public override ViewEngineResult FindView(ControllerContext controllerContext, string viewName, string masterName, bool useCache)
    {
        string viewTemplatePath = "~/Content/Sites/magical/" + viewName + ".aspx";
        string masterTemplatePath = string.Empty;
        return new ViewEngineResult(
            this.CreateView(controllerContext, viewTemplatePath, masterTemplatePath),
            this
        );
    }
}

模板如下所示:


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Plain.Master" Inherits="System.Web.Mvc.ViewPage<MySoln.Client.Presentation.MyPresenter>" %>
...
<%: Model.SomePresenterSpecificMember %>

如果我将强类型声明保留在声明的Inherits属性中,Page则会出现以下异常:

解析器错误消息:无法加载类型“System.Web.Mvc.ViewPage<MySoln.Client.Presentation.MyPresenter>”。

但是,如果我将模板更改为使用弱类型页面模型,而是在模板本身的 Model 成员上使用强制转换,则它可以工作:


<%@ Page Title="" Language="C#" MasterPageFile="~/Views/Shared/Plain.Master" Inherits="System.Web.Mvc.ViewPage" %>
...
<% var omg = (MySoln.Client.Presentation.MyPresenter) Model; %>
<%: omg.SomePresenterSpecificMember %>

所以,我的问题是,为什么前者的 barf 和后者的工作?我宁愿不要在每个模板顶部的标记中将 Model 转换为我的演示者类型之一。

谢谢!

4

1 回答 1

1

只需确保在自定义视图引擎路径的根目录中有以下 web.config 文件:

<?xml version="1.0"?>

<configuration>
  <system.web>
    <httpHandlers>
      <add path="*" verb="*" type="System.Web.HttpNotFoundHandler"/>
    </httpHandlers>

    <!--
        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.
    -->
    <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>

您可以将默认模板自动生成的 web.config 文件复制粘贴~/views/web.config~/content/web.config.

基本上重要的部分是:

pageBaseType="System.Web.Mvc.ViewPage, System.Web.Mvc, ..."

于 2010-09-17T17:53:09.773 回答