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 をプレゼンター タイプの 1 つにキャストしたくありません。

ありがとう!

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 に答える