@using(Html.BeginForm())返回格式异常mvc4

时间:2013-04-12 22:46:46

标签: asp.net-mvc-4 web-config iis-7.5

我的devC机器上运行的MVC4 Web应用程序没有任何问题,但是一旦我将其部署到主机服务器,我就会在@using(Html.BeginForm())上抛出以下异常。


        String was not recognized as a valid Boolean.

    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: System.FormatException: String was not recognized as a valid Boolean.

    Source Error: 


    Line 4:  
    Line 5:  
    Line 6:      @using(Html.BeginForm(new { returnurl = ViewBag.Returnurl }))
    Line 7:      {
    Line 8:          @Html.AntiForgeryToken()

    Source File: e:\Sites\familybook\Views\Login\Login.cshtml    Line: 6 

    Stack Trace: 


    [FormatException: String was not recognized as a valid Boolean.]
       System.Boolean.Parse(String value) +12636900
       System.Convert.ChangeType(Object value, Type conversionType, IFormatProvider provider) +12819466
       System.Web.Mvc.ViewContext.ScopeGet(IDictionary`2 scope, String name, TValue defaultValue) +89
       System.Web.Mvc.ScopeCache..ctor(IDictionary`2 scope) +48
       System.Web.Mvc.ScopeCache.Get(IDictionary`2 scope, HttpContextBase httpContext) +282
       System.Web.Mvc.ViewContext.GetClientValidationEnabled(IDictionary`2 scope, HttpContextBase httpContext) +9
       System.Web.Mvc.Html.FormExtensions.FormHelper(HtmlHelper htmlHelper, String formAction, FormMethod method, IDictionary`2 htmlAttributes) +162
       System.Web.Mvc.Html.FormExtensions.BeginForm(HtmlHelper htmlHelper, Object routeValues) +192
       ASP._Page_Views_Login_Login_cshtml.Execute() in e:\Sites\familybook\Views\Login\Login.cshtml:6
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +280
       System.Web.Mvc.WebViewPage.ExecutePageHierarchy() +125
       System.Web.WebPages.StartPage.ExecutePageHierarchy() +143
       System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +181
       System.Web.Mvc.ViewResultBase.ExecuteResult(ControllerContext context) +380
       System.Web.Mvc.c__DisplayClass1a.b__17() +33
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultFilter(IResultFilter filter, ResultExecutingContext preContext, Func`1 continuation) +613
       System.Web.Mvc.ControllerActionInvoker.InvokeActionResultWithFilters(ControllerContext controllerContext, IList`1 filters, ActionResult actionResult) +263
       System.Web.Mvc.Async.c__DisplayClass25.b__22(IAsyncResult asyncResult) +230
       System.Web.Mvc.c__DisplayClass1d.b__18(IAsyncResult asyncResult) +28
       System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +20
       System.Web.Mvc.Controller.EndExecuteCore(IAsyncResult asyncResult) +53
       System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +20
       System.Web.Mvc.c__DisplayClass8.b__3(IAsyncResult asyncResult) +42
       System.Web.Mvc.Async.c__DisplayClass4.b__3(IAsyncResult ar) +20
       System.Web.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute() +469
       System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously) +375

    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.272

主机服务器是Windows Server 2008 R2 64位,带有IIS 7.5,并且安装了最新的操作系统和.NET 4框架更新。

有趣的是,在我部署之前,我没有得到此错误,直到我使用Windows更新更新服务器。我注意到的另一件事是,如果我将发布类型设置为调试,则应用程序在没有任何问题的情况下工作,而不是发布发布类型。

我也尝试在我的开发机器上部署到本地文件夹,然后将内容复制到主机服务器,但得到了相同的最终结果。

编辑:

 @model FamilyBook.Models.Account.LoginUserModel

@Scripts.Render("~/bundles/jqueryval")

<section>
    @using(Html.BeginForm(new { returnurl = ViewBag.Returnurl }))
    {
        @Html.AntiForgeryToken()
        @Html.ValidationSummary(true)

        <fieldset>
            <legend>Login with local account</legend>

            <div class="editor-label">
                @Html.LabelFor(model => model.Username)
            </div>
            <div class="editor-field">
                @Html.EditorFor(model => model.Username)
                @Html.ValidationMessageFor(model => model.Username)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.Password)
            </div>
            <div class="editor-field">
                @Html.PasswordFor(model => model.Password)
                @Html.ValidationMessageFor(model => model.Password)
            </div>

            <div class="editor-label">
                @Html.LabelFor(model => model.RememberMe)
            </div>
            <div class="editor-field">
                @Html.CheckBoxFor(model => model.RememberMe)
            </div>

            <p>
                <input type="submit" value="Login" />
            </p>

        </fieldset>
    }
</section>

1 个答案:

答案 0 :(得分:2)

问题的根本原因是,当appSettings对自定义键执行web.conf转换时,它们用第一个自定义键的相同值覆盖所有其他键。所以对 - &gt;进行简单的更改解决了这个问题。 这可以在堆栈跟踪中看到,当调用MVC System.Web.Mvc.ViewContext.GetClientValidationEnabled中的此函数以检查是否启用了clientSideValidation时,应用程序崩溃。可在此主题http://forums.asp.net/t/1924833.aspx/1?Format+exception+on+Html+BeginForm+String+was+not+recognized+as+a+valid+boolean

中找到更多信息