Stringbuilder.ToString()在javascript中转换“和'符号生成'和”

问题描述 投票:0回答:2

我在StringBuilder中写了一些动态javascript代码,但是当我将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(&#39;ecommerce:addItem&#39;, {
id:&quot;HGT678&quot;,       // Order ID
sku:&quot;HI789&quot;,                                     // SKU
name:&#39;Test Item 456&#39;,                                  // Product Name 
category:&quot; &quot;,                                    // Category
price:&quot;337.0000&quot;,                                    // Price
quantity:&quot;1&quot;                                         // 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
});

需要有关此问题的帮助。

谢谢, 沙

javascript c# stringbuilder
2个回答
3
投票

如果您在ASP.NET MVC中,默认情况下会转义字符串,请尝试使用Html.Raw

@Html.Raw(gastring.ToString())

-1
投票

如果要在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>

© www.soinside.com 2019 - 2024. All rights reserved.