ASP.NET MVC2の標準以外の場所に強力なタイプのビューWebforms .aspxテンプレートを配置するにはどうすればよいですか?

StackOverflow https://stackoverflow.com/questions/3737549

質問

だから、私は個人的にこれが一種の強打だと思います。

.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をロードできませんでしたu003CMySoln.Client.Presentation.MyPresenter>'。

ただし、テンプレートを変更して弱いタイプのページモデルを使用し、代わりにテンプレート自体のモデルメンバーでキャストを使用する場合、次のように機能します。


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

それで、私の質問は、なぜ前のbarと後者が機能するのですか?私は、すべてのテンプレートの上部にあるタグで、プレゼンタータイプの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>

デフォルトのテンプレートによって自動的に生成され、 ~/views/web.config の中へ ~/content/web.config.

基本的に重要な部分は次のとおりです。

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

ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top