MVC4 プロジェクトを MVC5 に変換する作業を行っています。初日に「Microsoft.CSharp.RuntimeBinder.RuntimeBinderException」に遭遇しましたが、変換をやり直すことで解決できました。それが再び起こったので、何が残念な修正だったのかわかりません。
Login.cshtml ページを読み込むと、_ExternalLoginsListPartial.cshtml でエラーが発生します。エラーは 15 行目でスローされます。 (string action = Model.Action;)
@using Microsoft.Owin.Security
@{
var loginProviders = Context.GetOwinContext().Authentication.GetExternalAuthenticationTypes();
var authenticationDescriptions = loginProviders as AuthenticationDescription[] ?? loginProviders.ToArray();
if (!authenticationDescriptions.Any())
{
<div>
<p>There are no external authentication services configured. See <a href="http://go.microsoft.com/fwlink/?LinkId=313242">this article</a>
for details on setting up this ASP.NET application to support logging in via external services.</p>
</div>
}
else
{
string action = Model.Action;
string returnUrl = Model.ReturnUrl;
using (Html.BeginForm(action, "Account", new { ReturnUrl = returnUrl }))
{
@Html.AntiForgeryToken()
<div id="socialLoginList">
<p>
@foreach (AuthenticationDescription p in authenticationDescriptions)
{
<button type="submit" class="btn btn-default padded-8 margin-8" id="@p.AuthenticationType" name="provider"
value="@p.AuthenticationType" title="Log in using your @p.Caption account">
<img src="@Url.Content("~/Content/Brands/"+p.Caption+".png")" alt="Microsoft" class="img-responsive" />
<br/>
<b>@p.Caption</b>
</button>
}
</p>
</div>
}
}
}
スローされるエラーは
タイプ 'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException' の例外が System.Core.dll で発生しましたが、ユーザー コードでは処理されませんでした
追加情報:「オブジェクト」には「アクション」の定義が含まれていません
スナップショットは言う
メッセージ: object' には 'Action' の定義が含まれていません ソース: Anonymously Hosted DynamicMethods Assembly
ブレークポイントを設定するとModel.Actionがnullではないため、これは二重に奇妙です。価値がわかります。
これは本当にイライラします。アプリは 5 分前に動作していました.関連のないページの html を変更しました..そして今は動作しません。
ハック的な修正 このエラーが発生する理由を知りたいです。そうは言っても、他の誰かがこれに遭遇した場合に備えて、簡単な修正があります(これはデフォルトのソリューションの一部であるため)。解決策は、ダイナミクスを使用しないことです。独自のビューモデルを作成して渡します。
public class ExternalLoginViewModel
{
[Display(Name = "ReturnUrl")]
public string ReturnUrl { get; set; }
[Required]
[Display(Name = "Action")]
public string Action { get; set; }
}
@Html.Partial("_ExternalLoginsListPartial", new ExternalLoginViewModel { Action = "ExternalLogin", ReturnUrl = ViewBag.ReturnUrl })