【问题标题】:SET on html tag lang attribute asp.net-mvc在 html 标签 lang 属性 asp.net-mvc 上设置
【发布时间】:2018-12-06 02:09:24
【问题描述】:

如何在 MVC 中的 html 标记上设置(不做可怕的 hack)lang 属性

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
</html>

在 Forms 3.5 中,这可以通过将 runat='server' 添加到 html 标记然后设置值来完成。
我找不到这样做的“首选”方式。
我打算将它与css:lang 选择器一起使用

【问题讨论】:

标签: html asp.net-mvc lang


【解决方案1】:

@Vimal Stan 在评论中提出了最简单和最“正确”的方法。

<html 
xmlns="http://www.w3.org/1999/xhtml" 
xml:lang="en"  
lang="@System.Threading.Thread.CurrentThread.CurrentUICulture.TwoLetterISOLanguageName">

【讨论】:

  • 我发现如果您缺少所请求页面的本地化资源,此解决方案将不起作用。线程语言将正确地“解析”为Startup.cs 中定义的支持的本地化,但由于您缺少本地化,最终使用的本地化将是默认语言,而不是更新线程CurrentUICulture。最后,这是将lang 属性设置为错误的语言。鉴于这种情况,我正在尝试找到更好的解决方案,但现在我运气不佳。
【解决方案2】:

C#

ElementID.Attributes.Add("attributeyoucareabout","value to give it");

【讨论】:

  • 嗨,艾伦,感谢您的回答。原谅我的无知,但很少有事情对我来说并不完全清楚。这是表格的答案吗?您能否指出我如何访问 .NET MVC 中的元素。谢谢。
猜你喜欢
  • 2023-02-04
  • 2020-08-02
  • 2011-10-28
  • 2016-05-13
  • 2016-03-13
  • 2018-06-22
  • 1970-01-01
  • 2013-01-07
  • 1970-01-01
相关资源
最近更新 更多