在构建ASP.NET Core MVC应用程序(最近从.NET Core 2.2迁移到3.0)时,我们遇到以下错误:
标记帮助程序“输入”(或“文本区域”或任何其他)不得具有C# 在元素的属性声明区域中。
我们使用Razor @functions
返回HTML属性内容以解决该问题,但是当您使用变量从函数返回变量而没有任何额外逻辑时,它看起来很丑陋(函数dummy(htmlAttributeContent)
return {{1 }})
htmlAttributeContent
实际上我们遇到了错误
标记帮助程序'textarea'的元素属性中不得包含C# 申报区。
在编译我们的ASP.NET Core MVC应用程序时,我们需要获得一种方法(最好不要使用@{
var roAttrHTML = "";
if (!user.GotAccessToken("someToken")) {
roAttrHTML = " readonly=\"readonly\" ";
}
}
<textarea class="testClass" asp-for="testId" @readonlyAttribute>@Model.Id</textarea>
),这将为我们提供一种解决该问题的方法(因为我们有很多页面具有相似的逻辑,需要触摸一次,并避免在将来的.NET Core版本中对属性支持的可能的新更改而引起任何可能的问题
答案 0 :(得分:1)
你可以这样做:
def func():
print("\nPath 1 - bookkeeper", "\n Path 2 - mississippi",
"\n Path 3 - sleeplessness", "\n Path 4 - keenness",
"\n Path 5 - suddenness")
path_way = input("Scan the word you want by "
"\ntyping here to figure out the number associated "
"\nwith each letter to add them up : ")
foreign_lang = []
occurences = []
for character in path_way:
if character not in foreign_lang:
foreign_lang.append(character)
occurences.append(1)
else:
foreign_index = foreign_lang.index(character)
occurences[foreign_index] = occurences[foreign_index] + 1
for index in range(len(foreign_lang)):
print(foreign_lang[index], "-->", occurences[index])
print("Now that you added up you numbers use that number and")
while True:
func()
try:
act_3 = int(input("Enter the number of henchmen you want to fight : "))
if act_3 == 8:
print("\n You easily fight the 8 henchmen and defeat them to proceed with the path!")
break
else:
print("Sorry you have been killed as the henchmen overwhelmed you as there were too many of them")
except:
print( "Sorry you have been kicked from the agency for quitting on the mission")
答案 1 :(得分:0)
在创建新的3.0应用程序时,我遇到了同样的问题。一个旧的2.2应用程序允许我在声明中使用C#,即使“无效” 1 。
我所做的是使用以下步骤编写自己的TagHelper 2 :
CustomAttributeTagHelper .cs
namespace {YourBaseNameSpace}.Helpers.TagHelpers
{
using System.Collections.Generic;
using Microsoft.AspNetCore.Razor.TagHelpers;
[HtmlTargetElement(Attributes = "custom-attributes")]
public class CustomAttributeTagHelper : TagHelper
{
public Dictionary<string, string> CustomAttributes { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
if (CustomAttributes != null)
foreach (var pair in CustomAttributes)
if (!output.Attributes.ContainsName(pair.Key))
output.Attributes.Add(pair.Key, pair.Value);
}
}
}
/Pages/Shared/_ViewImports.cshtml
,将以下行添加到Microsoft自己的@addTagHelper
下@addTagHelper {YourBaseNameSpace}.Helpers.TagHelpers.*, {Your Assembly Name}
关键点:确保您使用程序集名称而不是上面逗号后面的名称空间
2a。如果不确定您的程序集名称是什么,请在Program
=> Main(string[] args)
方法中运行以下代码:
Type t = typeof({YourBaseNameSpace}.Helpers.TagHelpers.CustomAttributeTagHelper);
string s = t.Assembly.GetName().Name.ToString();
Console.WriteLine($"The fully qualified assembly name you need is: {s}.");
@{
Dictionary<string, string> myCustomAttributes = new Dictionary<string, string> {
["data-first-custom-attribute"] = "my custom value"
};
if (!user.GotAccessToken("someToken")) {
myCustomAttributes.Add("readonly","readonly");
}
}
<input custom-attributes="myCustomAttributes" />
注意:此代码可在任何TagHelper上使用,意味着您可以添加任何所需的自定义属性。
如果您只想为单个属性(如上面的只读内容)创建它,则可以创建一个条件TagHelper,该条件在构造时或其值上需要布尔值。
答案 2 :(得分:0)
对于同一问题,但具有多个属性,我做了以下解决方法:
@{
//...
string attributes = "...prepare attributes ...";
string tag = "<input " + attributes + " />";
<text>@Html.Raw(tag)</text>
//...
}
由于某些安全漏洞,不建议使用@ Html.Raw,但我仍在这里使用它。注意这种风险。如果需要,我可以分享有关该风险的其他文章。否则,对于单个属性,我同意@Rena的评论。
答案 3 :(得分:0)
我最终删除了该行
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
来自_ViewImports.cshtml
在我的情况下,我必须非常精确地构建html,而且我还是没有使用TagHelpers。
答案 4 :(得分:0)
如果您不需要使用 TagHelper,您可以使用 <!elementName>
为特定元素禁用它:
<!textarea class="testClass" asp-for="testId" @readonlyAttribute>@Model.Id</!textarea>
请参阅 @glenn223's answer 以获得更结构化的解决方案。我改进了他的解决方案,添加了对匿名对象的支持:
using Microsoft.AspNetCore.Mvc.ViewFeatures;
using Microsoft.AspNetCore.Razor.TagHelpers;
namespace {YourBaseNameSpace}.Helpers.TagHelpers
{
[HtmlTargetElement(Attributes = "custom-attributes")]
public class CustomAttributesTagHelper : TagHelper
{
public object CustomAttributes { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
var customAttributesDictionary = HtmlHelper.AnonymousObjectToHtmlAttributes(CustomAttributes);
foreach (var (key, value) in customAttributesDictionary)
{
output.Attributes.SetAttribute(key, value);
}
}
}
}