Search code examples
asp.net-mvc-4web-configiis-7.5

@using(Html.BeginForm()) returning Format exception mvc4


I have an MVC4 web app running without any issues on my dev machine but once i deploy it to the host server i get the following exception thrown on @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

The host server is a Windows server 2008 R2 64-bit with IIS 7.5 and the latest updates for OS and .NET 4 framework installed on it.

The funny thing is that when I deployed before I did not get this error until I updated the server with windows updates. Another thing that I have noted is that if I set the type of publish to debug the application works without any problems while it does not with release publish type.

I did also try to deploy to local folder on my dev machine and then copy the content to the host server, but got the same end results.

Edit:

 @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>

Solution

  • The root cause of the problem was that when web.conf transformation was performed on appSettings for custom keys, they overwrote all other keys with the same value of the first custom key. So a simple change on the -> solved the problem. This can be seen in the stack trace where the application crashes when this function in MVC System.Web.Mvc.ViewContext.GetClientValidationEnabled is called to check if clientSideValidation is enabled or not. More info can be found in this thread http://forums.asp.net/t/1924833.aspx/1?Format+exception+on+Html+BeginForm+String+was+not+recognized+as+a+valid+boolean