【问题标题】:DataAnnotations - Validation not happeningDataAnnotations - 验证未发生
【发布时间】:2023-03-03 14:41:01
【问题描述】:

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

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

DataAnnotationsExtensions.ClientValidation.dll
DataAnnotationsExtensions.dll

在服务器端,跟随应该失败,但它没有发生。

!TryValidateModel(model) || !ModelState.IsValid

本站使用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>

出于测试目的,我目前不使用捆绑包。对于捆绑包,我什至用以下方法对其进行了测试

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

我还引用了以下 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>

该应用程序是 MVC 5 并且所有已通过 NuGet 包添加。我没有在服务器上安装 MVC。我在这些服务器上有另一个 MVC 5 应用程序,验证进行得很好。

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

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

在旧的暂存和生产盒子上,验证工作正常,但后来我们安装了 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

            }
        }

更新 2 - HTML 差异

看起来验证属性甚至没有进入 html,好像网站甚至不知道我们正在使用验证。现在,开发站点和登台站点都具有相同的代码。

临时站点

<input autofocus="autofocus" class="clearSearchFields" id="Search_Key" maxlength="6" name="Search.Key" size="6" type="text" value="" /><br />

工作开发站点

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

【问题讨论】:

  • 你能显示控制器吗
  • 控制台有错误吗?还有一个愚蠢的问题——你有没有在页面中添加 jquery?
  • 我已经用控制器代码更新了案例。控制台显示没有错误。我添加了 jquery,本地开发和开发站点都在工作。
  • 你在 html 元素中看到data-attributes 吗?
  • @AmirHosseinMehrvarzi 在开发上是,在登台上不行...更新 2 显示了开发和登台的 html。目前,开发站点和临时站点都具有相同的代码库。

标签: asp.net-mvc validation data-annotations


【解决方案1】:

由于您在该机器上有另一个运行良好的 mvc 5 应用程序并且您没有安装 MVC,因此似乎某些东西没有正确部署。很可能在 MVC 包中需要一些您没有的程序集。

是否有不能在服务器上安装 MVC 的原因?有可用的独立软件包。这应该会将您需要的所有内容添加到 GAC。

如果您无法安装 MVC,我会查看您正在运行的 MVC 5 应用程序的 bin。它似乎比您的新应用程序拥有更多的 .Net 程序集?如果是这样,那么可能有人在其中包含了所有缺少的 MVC 程序集。您可以尝试从工作的 mvc 应用程序复制所有程序集,只要确保您没有覆盖。这应该会显示您缺少的任何程序集。

【讨论】:

  • 我无法在服务器上安装 MVC,所有东西都需要放在 bin 中。 MVC 也没有安装在开发盒上,它在那里工作。我再看看 bin 文件夹,看看有没有发现。
  • 比较两个垃圾箱有什么运气吗?
  • 没有。我在我的问题线程中添加了更新#2。在查看 html 时,我没有看到与验证相关的 html。开发站点和暂存站点都具有相同的代码库。
  • 请在问题线程下查看我的 web.config 相关评论。
【解决方案2】:

由于一些发帖者没有阅读完整的问题和 cmets 并尝试回答,我将我的最后两个 cmets 从问题线程中移出作为答案。我的问题已解决。

我将 web.config 从我的本地机器移到了 staging 和 prod,并且验证开始工作。我检查了旧的 web.config 和这个新的工作文件夹 web.config 并且没有区别。即使它正在工作,我很高兴,但我现在同时感到困惑。

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

【讨论】:

    猜你喜欢
    • 2011-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-04
    • 2012-08-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多