Stringbuilder.ToString() 在 javascript 中转换 " 和 ' 符号生成 ' 和 "
Stringbuilder.ToString() Converting " and ' symbol generate ' and " in javascript
我将一些动态 javascript 代码写入 StringBuilder,但是当我将 StringBuilder 对象转换为字符串时,它将符号转换为 html 代码。下面是我的代码:
System.Text.StringBuilder gastring = new System.Text.StringBuilder();
@foreach (var line in OrderLines)
{
gastring.AppendLine("ga('ecommerce:addItem', {");
gastring.AppendLine("id:\"" + line.OrderNumber + "\", // Order ID");
gastring.AppendLine("sku:\"" + line.SkuCode + "\", // SKU");
gastring.AppendLine("name:" + Microsoft.Security.Application.AntiXss.JavaScriptEncode(line.SkuName) + ", // Product Name ");
gastring.AppendLine("category:\"" + " " + "\", // Category");
gastring.AppendLine("price:\"" + line.UnitPrice + "\", // Price");
gastring.AppendLine("quantity:\"" + line.Qty + "\" // Quantity");
gastring.AppendLine("});");
}
<script type="text/javascript">
gastring.ToString()
ga('ecommerce:send');
</script>
但是当我运行这段代码时,gastring.ToString()
行生成javascript代码如下:
ga('ecommerce:addItem', {
id:"HGT678", // Order ID
sku:"HI789", // SKU
name:'Test Item 456', // Product Name
category:" ", // Category
price:"337.0000", // Price
quantity:"1" // Quantity
});
预期输出应如下所示:
ga('ecommerce:addItem', {
id:"HGT678", // Order ID
sku:"HI789", // SKU
name:"Test Item 456", // Product Name
category:" ", // Category
price:"337.0000", // Price
quantity:"1" // Quantity
});
在这个问题上需要帮助。
谢谢,
桑迪
如果您在 ASP.NET MVC 中,默认情况下会转义字符串,请尝试 Html.Raw
@Html.Raw(gastring.ToString())
如果要在 AppendLine() 之间添加任何引号,则必须在单引号和双引号之前附加转义字符 (\)。
System.Text.StringBuilder gastring = new System.Text.StringBuilder();
foreach (var line in OrderLines)
{
gastring.AppendLine("ga(\'ecommerce:addItem\', {");
gastring.AppendLine("id:\""+ line.OrderNumber +"\", // Order ID");
gastring.AppendLine("sku:\""+ line.SkuCode +"\", // SKU");
gastring.AppendLine("name:\""+ Microsoft.Security.Application.AntiXss.JavaScriptEncode(line.SkuName) + "\", ");
gastring.AppendLine("category:\""+ +"\" , // Category");
gastring.AppendLine("price:\""+ line.UnitPrice +"\", // Price");
gastring.AppendLine("quantity:\" "+ line.Qty +"\" // Quantity");
gastring.AppendLine("});");
}
<script type="text/javascript">
gastring.ToString()
ga('ecommerce:send');
</script>
我将一些动态 javascript 代码写入 StringBuilder,但是当我将 StringBuilder 对象转换为字符串时,它将符号转换为 html 代码。下面是我的代码:
System.Text.StringBuilder gastring = new System.Text.StringBuilder();
@foreach (var line in OrderLines)
{
gastring.AppendLine("ga('ecommerce:addItem', {");
gastring.AppendLine("id:\"" + line.OrderNumber + "\", // Order ID");
gastring.AppendLine("sku:\"" + line.SkuCode + "\", // SKU");
gastring.AppendLine("name:" + Microsoft.Security.Application.AntiXss.JavaScriptEncode(line.SkuName) + ", // Product Name ");
gastring.AppendLine("category:\"" + " " + "\", // Category");
gastring.AppendLine("price:\"" + line.UnitPrice + "\", // Price");
gastring.AppendLine("quantity:\"" + line.Qty + "\" // Quantity");
gastring.AppendLine("});");
}
<script type="text/javascript">
gastring.ToString()
ga('ecommerce:send');
</script>
但是当我运行这段代码时,gastring.ToString()
行生成javascript代码如下:
ga('ecommerce:addItem', {
id:"HGT678", // Order ID
sku:"HI789", // SKU
name:'Test Item 456', // Product Name
category:" ", // Category
price:"337.0000", // Price
quantity:"1" // Quantity
});
预期输出应如下所示:
ga('ecommerce:addItem', {
id:"HGT678", // Order ID
sku:"HI789", // SKU
name:"Test Item 456", // Product Name
category:" ", // Category
price:"337.0000", // Price
quantity:"1" // Quantity
});
在这个问题上需要帮助。
谢谢,
桑迪
如果您在 ASP.NET MVC 中,默认情况下会转义字符串,请尝试 Html.Raw
@Html.Raw(gastring.ToString())
如果要在 AppendLine() 之间添加任何引号,则必须在单引号和双引号之前附加转义字符 (\)。
System.Text.StringBuilder gastring = new System.Text.StringBuilder();
foreach (var line in OrderLines)
{
gastring.AppendLine("ga(\'ecommerce:addItem\', {");
gastring.AppendLine("id:\""+ line.OrderNumber +"\", // Order ID");
gastring.AppendLine("sku:\""+ line.SkuCode +"\", // SKU");
gastring.AppendLine("name:\""+ Microsoft.Security.Application.AntiXss.JavaScriptEncode(line.SkuName) + "\", ");
gastring.AppendLine("category:\""+ +"\" , // Category");
gastring.AppendLine("price:\""+ line.UnitPrice +"\", // Price");
gastring.AppendLine("quantity:\" "+ line.Qty +"\" // Quantity");
gastring.AppendLine("});");
}
<script type="text/javascript">
gastring.ToString()
ga('ecommerce:send');
</script>