香港专业教育学院一直致力于将MVC4项目转换为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>
        }
    }
}


引发的错误是


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

附加信息:“对象”不包含“动作”的定义


快照说


消息:对象”不包含“动作”的定义
来源:匿名托管的DynamicMethods程序集


现在这很奇怪,因为当我设置断点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 })

最佳答案

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

因此,将来任何人读到这篇文章:尝试将Visual Studio 2013更新为至少更新2。

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

关于asp.net-mvc - MVC5 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/19626425/

10-12 14:55
查看更多