0

のdbconfigに基づいてレイアウトが設定されているサイトがあります

~/Views/_ViewStart.cshtml like so
@{
    Layout = ViewContext.ViewBag.MyConfig.ThemeName;
}

そして、独自の ViewStart を使用して Emails フォルダーをビューに追加した場合 ( Postal Nuget Package の場合)を除いて、すべて正常に機能しています

~/Views/Emails/_ViewStart.cshtml 

を含む

@{ Layout = null; /* Overrides the Layout set for regular page views. */ }

HTML形式のメールをコードで送信するために使用されます

        dynamic email = new Email("<nameofView>"); // this is in folder ~/Views/Emails/
        email.To = to;
        .... other fields....
        email.Send();

ただし、この行で例外が発生します

    Layout = ViewContext.ViewBag.MyConfig.ThemeName;


 Cannot perform runtime binding on a null reference
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference

Source Error:


Line 1:  @{
Line 2:      Layout = ViewContext.ViewBag.MyConfig.ThemeName;
Line 3:  }

~/Views/Emails からではなく ~/Views から ViewStart を取得する理由についての指針はありますか?

4

1 に答える 1

1

「ルート」も使用され~/Views/Emails/_ViewStart.html ます_ViewStartが、事前に実行されていることを覚えておく必要があります。

がスローされ_ViewStartないようにするには、ルートを変更するだけです。Exception

@{
    Layout = ViewContext.ViewBag.MyConfig != null ? ViewContext.ViewBag.MyConfig.ThemeName : null;
}

あなた~/Views/Emails/_ViewStart.htmlは従い、あなたをLayout適切に設定します。

于 2013-06-04T20:03:35.433 に答える