他们是否可以将字符串(此字符串可以更改并包含 asp-route -... 属性)转换为html属性列表?剃刀引擎应该使用所有asp-route -...属性来转换为正确的url。我有以下代码,但这不起作用。
@{
var Attributes = ViewData["Attributes"] as Dictionary<string,string>;
var AttributeRoute = "";
@foreach (var key in Attributes.Keys)
{
AttributeRoute += "asp-route-"+key+"=\""+Attributes[key]+"\" ";
}
}
...
@AttributeRoute #Prints output (ex. asp-route-testkey="testvalue")
<a class='item' @AttributeRoute>test</a> #Doesn't print the list of attributes
答案 0 :(得分:0)
通过以下方式自行解决:
Save console output to a file
namespace test
{
[HtmlTargetElement("special-link")]
public class SpecialLinkTagHelper : TagHelper
{
[ViewContext]
public ViewContext ViewContext { get; set; }
public override void Process(TagHelperContext context, TagHelperOutput output)
{
//Create a tag
output.TagName="a";
//Get the parameters
string parameters="";
Dictionary<string,string> Parameters = (Dictionary<string,string>)this.ViewContext.ViewData["Attributes"];
foreach(KeyValuePair<string, string> pair in Parameters){
parameters+= pair.Key+"="+pair.Value+"&";
}
output.Attributes.SetAttribute("href", "?"+parameters);
}
}
}
希望这可以帮助别人!