我一直在努力將MVC4項目轉換爲MVC5。第一天我遇到了一個'Microsoft.CSharp.RuntimeBinder.RuntimeBinderException',但能夠通過開始我的轉換來解決它。我不確定這是一個什麼樣的問題,因爲它再次發生。MVC5 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException
加載Login.cshtml頁面時,_ExternalLoginsListPartial.cshtml中發生錯誤。該錯誤是在管線15拋出(串行動= 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」的例外.Core.dll但在用戶代碼中沒有處理
其他信息:「對象」不包含「操作」
在S的定義napshot說
消息:對象」不包含定義‘行動’ 來源:匿名運行DynamicMethods大會
現在,這是雙重奇怪,因爲當我設置一個斷點Model.Action不空值。我可以看到價值。
這真令人沮喪。該應用程序工作5分鐘前..我已經改變了非關聯頁面上的HTML ..現在它不會工作。
Hackish Fix 我想知道爲什麼會出現這個錯誤。也就是說,我有一個快速修復,以防其他人遇到這種情況(因爲這是默認解決方案的一部分)。解決方案是不使用動態。創建您自己的視圖模型並通過它。
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 })
我有完全相同的問題。很奇怪。 – par
我設法通過簡單地傳遞動作字符串並在_ExternalLoginsListPartial中直接使用ViewBag.ReturnUrl來解決問題 - 而不是創建新的ViewModel。 – par
你知道底層問題是什麼嗎?我記得我也有這個問題,但是這是因爲我的ViewModels沒有正確引用cshtml;但現在它又一次發生了,我無法弄清楚什麼是錯的! – teh0wner