简体   繁体   English

HTML注释中的Razor代码

[英]Razor code within HTML Comments

I have the following piece of code on my Razor page: 我的Razor页面上有以下代码:

<ul id="product-tabs-nav"><!--
    --><li><a href="#product-summary">Summary</a></li><!--
    @if (AppSettings.EcommerceEnabled)
    {
        --><li><a href="#product-reviews">Reviews</a></li><!--
    }
--></ul>

but it is not working as it seems to be commenting out my razor code (which I thought you would have had to use razor comment tags for). 但是它不起作用,因为它似乎注释掉了我的剃须刀代码(我认为您必须为此使用剃须刀注释标签)。 Does anyone know how to make this work as I need to comment out the white space (I want the li tags centrally aligned and the only way to do this is to make them inline-block elements which will add the space if I have white-space between them) 有谁知道如何做这个工作,因为我需要注释掉空白(我想让li标签居中对齐,唯一的方法是使它们inline-block元素,如果我有空白,则会添加空格,他们之间的空间)

Ok after much playing around with this I have found the error to be with the closing comment tags within the Razor if statement. 好了,在进行了很多尝试之后,我发现错误与Razor if语句中的结束注释标记有关。

I think as it starts with a dash, the compiler is treating this as Razor syntax instead of taking it back to HTML content. 我认为从破折号开始,编译器将其视为Razor语法,而不是将其带回HTML内容。 I have solved the problem using Html.Raw : 我已经使用Html.Raw解决了这个问题:

<ul id="product-tabs-nav"><!--
     --><li><a href="#product-summary">Summary</a></li><!--
     @if (AppSettings.EcommerceEnabled)
     {
         @Html.Raw("--><li><a href=\"#product-reviews\">Reviews</a></li><!--");
     }
 --></ul>

Comment in razor c# code you can create llike this @* *@ 用razor C#代码注释可以创建类似@* *@

So 所以

<ul id="product-tabs-nav"><!-- Some html comment   -->
<li><a href="#product-summary">Summary</a></li>
  @*if (AppSettings.EcommerceEnabled)
  { this is commented
      *@
<li><a href="#product-reviews">Reviews</a></li>
@*
  } this is commented
*@</ul>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM