【问题标题】:What is the ASP.NET Core equivalent of ViewData.TemplateInfo.GetFullHtmlFieldId("PropertyName")?ViewData.TemplateInfo.GetFullHtmlFieldId("PropertyName") 的 ASP.NET Core 等效项是什么?
【发布时间】:2019-10-22 15:12:40
【问题描述】:

我正在将一个 Web 应用程序迁移到 .NET Core,它使用一些 Razor 编辑器模板来处理特定的输入类型(例如,为任何 DateTime 模型属性输出一个 date 输入)。

一些模板使用以下方法获取 ID 属性值,以便在 HTML 中的其他地方使用:

ViewData.TemplateInfo.GetFullHtmlFieldId("PropertyName")

但是,这种方法在 ASP.NET Core 中似乎不再存在。

GetFullHtmlFieldName 方法仍然存在,因此可以通过以下方式获得相同的结果(至少对于我测试过的所有内容):

Regex.Replace(ViewData.TemplateInfo.GetFullHtmlFieldName("PropertyName"), @"[\.\[\]]", "_")

但这对我来说似乎有点不整洁,更不用说旧方法可能处理我不知道的边缘情况。

半小时的谷歌搜索、阅读 .NET Core docs 和搜索 SO 并没有发现任何有用的信息。我能找到的唯一与远程相关的是this answer(它只是确认该方法已消失)。

有人知道为什么GetFullHtmlFieldId 不再存在吗?这只是一个意外遗漏,还是现在有更新、更好的方法?

【问题讨论】:

    标签: c# asp.net-mvc asp.net-core razor .net-core


    【解决方案1】:

    我查看了 ASP.NET Core 存储库,由于对 HtmlHelper 的静态引用,它看起来像 TemplateInfo.GetFullHtmlFieldId()originally removed

    经过大量挖掘(.NET Core 源代码浏览器是一个令人惊奇有用的工具),我认为我的问题的解决方案是使用IHtmlHelper.GenerateIdFromName。所以,这段代码:

    ViewData.TemplateInfo.GetFullHtmlFieldId("PropertyName")
    

    现在应该写成:

    Html.GenerateIdFromName(ViewData.TemplateInfo.GetFullHtmlFieldName("PropertyName"))
    

    仍然不是那么整齐(或一致),但至少通过这种方式我们重用了框架用来构造其 ID 的the same internal logic

    【讨论】:

      【解决方案2】:

      根据您的需要模拟旧行为应该不难。一切都回到了TagBuilder.CreateSanitizedId,它也存在于 .NET Core 中,但它发生了变化,逻辑也发生了变化。

      TemplateInfo 类在System.Web.Mvc 中有以下内容:

      private string _htmlFieldPrefix;
      
      public string HtmlFieldPrefix
      {
          get { return _htmlFieldPrefix ?? String.Empty; }
          set { _htmlFieldPrefix = value; }
      }
      
      public string GetFullHtmlFieldId(string partialFieldName)
      {
          return HtmlHelper.GenerateIdFromName(GetFullHtmlFieldName(partialFieldName));
      }
      
      public string GetFullHtmlFieldName(string partialFieldName)
      {
          // This uses "combine and trim" because either or both of these values might be empty
          return (HtmlFieldPrefix + "." + (partialFieldName ?? String.Empty)).Trim('.');
      }
      

      然后它使用HtmlHelper从名称生成id:

      public static string IdAttributeDotReplacement
      {
         get { return WebPages.Html.HtmlHelper.IdAttributeDotReplacement; }
         set { WebPages.Html.HtmlHelper.IdAttributeDotReplacement = value; }
      }
      
      public static string GenerateIdFromName(string name, string idAttributeDotReplacement)
      {
          if (name == null)
          {
              throw new ArgumentNullException("name");
          }
      
          if (idAttributeDotReplacement == null)
          {
              throw new ArgumentNullException("idAttributeDotReplacement");
          }
      
          // TagBuilder.CreateSanitizedId returns null for empty strings, return String.Empty instead to avoid breaking change
          if (name.Length == 0)
          {
              return String.Empty;
          }
      
          return TagBuilder.CreateSanitizedId(name, idAttributeDotReplacement);
      }
      

      根据您的需要需要研究和调整的内容:

      1. IdAttributeDotReplacement 属性(获取或设置用于替换呈现的表单控件的 id 属性中的点 (.) 的字符),它也存在于 .NET Core 中 Microsoft.AspNetCore.Mvc.Rendering
      2. TagBuilder.CreateSanitizedId(name, idAttributeDotReplacement) 我可以看到已修改为 TagBuilder.CreateSanitizedId(string name, string invalidCharReplacement);

      我真的不知道在 .NET Core 中成为其他东西的方法,但我期待着找到答案。

      【讨论】:

      • 这绝对帮助我找到了解决方案,所以即使评论框不鼓励“谢谢”cmets:谢谢!
      猜你喜欢
      • 2019-02-12
      • 2018-01-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-24
      相关资源
      最近更新 更多