DataAnnotations - 验证未发生

lea*_*... 6 validation asp.net-mvc data-annotations

验证在本地盒子,开发站点上正常工作但在登台和生产站点上没有发生.客户端和服务器端验证都没有发生.分段和生产都是负载平衡的,但由于某些其他功能要求而使用粘性连接.

我已经检查了所有环境中的bin文件夹,我在那里看到了以下两个dll.

DataAnnotationsExtensions.ClientValidation.dll
DataAnnotationsExtensions.dll
Run Code Online (Sandbox Code Playgroud)

在服务器端,以下应该失败但不会发生.

!TryValidateModel(model) || !ModelState.IsValid
Run Code Online (Sandbox Code Playgroud)

此站点使用Windows身份验证.

Web.config文件

<appSettings file="Configs\AppSettings_LocalHost.config">
        <add key="webpages:Version" value="3.0.0.0" />
        <add key="webpages:Enabled" value="false" />
        <add key="ClientValidationEnabled" value="true" />
        <add key="UnobtrusiveJavaScriptEnabled" value="true" />
    </appSettings>
Run Code Online (Sandbox Code Playgroud)

出于测试目的,我目前不使用捆绑包.对于捆绑包,我甚至用以下测试它

<location path="~/Content">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>
    <location path="~/bundles">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>
    <location path="~/Scripts">
        <system.web>
            <authorization>
                <allow users ="*" />
            </authorization>
        </system.web>
    </location>
Run Code Online (Sandbox Code Playgroud)

我也引用了以下JS文件

<script src="/NetSite/Scripts/Core/jquery.validate.min.js?v=1.12" type="text/javascript"></script>
<script src="/NetSite/Scripts/Core/jquery.validate.unobtrusive.min.js?v=1.12" type="text/javascript"></script>
<script src="/NetSite/Scripts/Custom/Validators.js?v=1.12" type="text/javascript"></script>
Run Code Online (Sandbox Code Playgroud)

该应用程序是MVC 5,所有都是通过NuGet包添加的.我没有在服务器上安装MVC.我在这些服务器上有另一个MVC 5应用程序,验证正常.

这里是表单标签,第二个工作应用程序使用相同的表单标签.

using (Html.BeginForm(ActionNames.Index, ControllerNames.Rankings, new { Area = AreaNames.MemberToolsReports }, FormMethod.Post, new { id = "RankingsSearchForm" }))
Run Code Online (Sandbox Code Playgroud)

在旧的分段和生产盒上,验证工作正常,但我们已经安装了MVC 3.

更新 - 控制器代码

using System;
        using System.Collections.Generic;
        using System.Linq;
        using System.Web;
        using System.Web.Mvc;
        using Project.BusinessEntities;
        using Project.Common.Constants;
        using Project.MvcBase;
        using Project.Resources;
        using Project.ServiceInterfaces;
        using Project.ViewModels;
        using Project.ViewModels.MemberToolReports;
        using Microsoft.Practices.Unity;
        using Project.Helpers.Helpers;
        using Project.Helpers.IO;

        namespace Project.Site.Areas.MemberToolsReports.Controllers
        {
            public class RankingsController : BaseController
            {
                #region PROPERTIES

                [Dependency]
                public IGeographyService GeographyServiceInstance { get; set; }

                [Dependency]
                public IRankingsService RankingsServiceInstance { get; set; }

                [Dependency]
                public IUtilityService UtilityServiceInstance { get; set; }

                #endregion

                #region ACTIONS

                public ActionResult Index()
                {
                    var states = getting states here
                    var key = String.Empty;

                    var search = new RankingSearch { Key = key };

                    var model = new RankingSearchViewModel { Search = search, StatesList = states };

                    return View(model);
                }

                [HttpPost]
                [ValidateAntiForgeryToken]
                public ActionResult Index(RankingSearchViewModel model)
                {
                    var errorModel = new ContentShowError { IsError = true };
                    var resultModel = new RankingsSearchResultsViewModel();

                    try
                    {   
                    //TODO: remove extra code once data annotations issue is fixed on staging and prod
                        if (!Request.IsAjaxRequest())
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorServicingRequest);
                        }
                        else if (!TryValidateModel(model) || !ModelState.IsValid)
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorProcessingRequest);
                        }
                        else if (String.IsNullOrWhiteSpace(model.Search.Key) &&
                                 String.IsNullOrWhiteSpace(model.Search.Institution) &&
                                 String.IsNullOrWhiteSpace(model.Search.State))
                        {
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.NoCriteriaSpecified);
                        }
                        else
                        {
                            //default - debug code
                            errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorNoDataFound);

                            var results = RankingsServiceInstance.SearchRanking(model.Search);
                            if (results != null && results.Count > 0)
                            {
                                errorModel.IsError = false;
                                errorModel.Message = String.Empty;

                                //update result model
                                resultModel.Rankings = results;

                            }
                        }
                    }
                    catch (Exception ex)
                    {
                        errorModel.Message = base.GetDisplayMessage(ProcessingMessagesEnum.ErrorProcessingRequest);
                        base.LogException(ex);
                    }
                    ActionResult result = null;
                    result = errorModel.IsError ? PartialView(ViewNames.ErrorControl, errorModel) : PartialView(ViewNames.SearchResultsControl, resultModel);

                    return result;
                }

                #endregion

            }
        }
Run Code Online (Sandbox Code Playgroud)

更新2 - HTML差异

看起来验证属性甚至没有进入html,好像该网站甚至不知道我们正在使用验证.现在,dev和staging网站都有相同的代码.

暂存网站

<input autofocus="autofocus" class="clearSearchFields" id="Search_Key" maxlength="6" name="Search.Key" size="6" type="text" value="" /><br />
Run Code Online (Sandbox Code Playgroud)

工作开发站点

<input autofocus="autofocus" class="clearSearchFields" data-val="true" data-val-length="Key must be 6 characters long" data-val-length-max="6" data-val-length-min="6" data-val-regex="Only alphanumeric (A-Z a-z 0-9) values are allowed" data-val-regex-pattern="[A-Za-z0-9]*" id="Search_Key" maxlength="6" name="Search.Key" size="6" type="text" value="" /><br />
                                <span class="field-validation-valid" data-valmsg-for="Search.Key" data-valmsg-replace="true"></span>
Run Code Online (Sandbox Code Playgroud)

lea*_*... 0

由于一些发帖者没有阅读完整的问题和评论并尝试回答,因此我将问题线程中的最后两条评论移至答案。我的问题已解决。

我将 web.config 从本地计算机移至暂存和生产,验证开始工作。我检查了旧的 web.config 和这个新的工作文件夹 web.config,没有任何区别。尽管它有效,我很高兴,但同时我也很困惑。

在这种情况下,ASP.NET 临时文件似乎是一个问题。当我手动更新 web.config 时,临时文件也得到更新,这解决了我的问题。