的Razor视图引擎会自动将报价? [英] Razor view engine automatically applying quotes?
问题描述
我很习惯现在的剃须刀,但我不明白为什么下面的语法是正确的?
I'm fairly used to razor now, but I can't understand why the following syntax is correct?
<li @(active ? "class=active" : "")>
@Html.ActionLink(item.Text, item.TargetAction, Model.Controller)
</li>
如你所见,我有条件应用类(和我都写过这样因此不生成的类标签,如果布尔活跃==假)。
As you can see I'm conditionally applying a class (and I've written it this way so the class tag is not generated if the bool active == false).
我能不明白是为什么这则生成正确的报价给:
What I can't understand is why this then generates the correct quotes to give:
<li class="active"><a href="/">Home</a></li>
<a href="/">Home</a>
</li>
不知怎的,它奇迹般地整理出报价,但我不能找到剃须刀上的文章表明这种预期的任何引用,所以我想知道如果它是依靠正常的行为。如果我添加单或双引号到字周围的字符串'积极',因为你会想到,如果制造出的HTML,我结束了:
Somehow it is magically sorting out the quoting, but I can't find any reference in the articles on razor to suggest this is expected, so I'm wondering if it is relying on broken behaviour. If I add single or double quotes into the string around the word 'active', as you would expect to if cranking out html, I end up with:
<li class="'active'">
<a href="/">Home</a>
</li>
或
<li class="active">
<a href="/">Home</a>
</li>
为什么它以这种方式工作,并且是我的code正确的(而不是简单地工作)?
Why does it work this way, and is my code correct (as opposed to simply functioning)?
推荐答案
剃刀自动HTML的转义所有code输出。结果
您可以通过编写prevent这个 @ Html.Raw(...)
Razor automatically HTML-escapes all code output.
You can prevent this by writing @Html.Raw(...)
另外,你可以把引号中的文字文本:
Alternatively, you can put the quotes in the literal text:
<li class="@(active ? "active" : "")>
您例子的作品,因为你实际上并没有任何的报价。结果
生成的HTML源代码读&LT;李类=有效方式&gt;
Your example works because you don't actually have any quotes.
The generated HTML source reads <li class=active>
.
这篇关于的Razor视图引擎会自动将报价?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!