MVC5 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException

nVe*_*lia 17 asp.net-mvc dynamic

我一直致力于将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>
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

抛出的错误是

System.Core.dll中出现"Microsoft.CSharp.RuntimeBinder.RuntimeBinderException"类型的异常,但未在用户代码中处理

附加信息:'object'不包含'Action'的定义

快照说

消息:对象'不包含'Action'的定义来源:匿名托管DynamicMethods程序集

现在这是双重奇怪的,因为当我设置断点时,Model.Action不为null.我可以看到价值.

这真令人沮丧.该应用程序在5分钟前工作..我已经更改了非相关页面上的HTML ..现在它不会工作.

Hackish Fix 我宁愿知道为什么会发生这种错误.也就是说,我有一个快速修复,以防其他任何人遇到这个(因为这是默认解决方案的一部分).解决方案是不使用动态.创建自己的viewmodel并传递它.

  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 })
Run Code Online (Sandbox Code Playgroud)

Amy*_*Amy 3

此错误已由 Microsoft 验证,他们正在努力修复它。

所以未来读到这篇文章的人:尝试将 Visual Studio 2013 更新到至少更新 2。

https://connect.microsoft.com/VisualStudio/feedback/details/813133/bug-in-mvc-5-framework-asp-net-identity-modules