VB - ASP.NET MVC 3 Razor 渲染额外空白
我在下面的锚标记水平列表中呈现了额外的空白字符。这给我带来了一个主要的 CSS 样式问题。
我知道就 C# 解决方法而言,这个问题已经得到解决(asp.net mvc剃刀额外空间),但是有人可以帮助我解决VB问题吗?我没有花括号的优势来消除单行 If 条件中的所有空格。
我现在已经设法通过编写 HtmlHelper 扩展方法来解决这个问题,但是这个动态代码仅在一个地方使用(主布局页面)。在我看来,这确实应该在 .vbhtml 页面中完成。也欢迎任何反对这一观点的论据。
这是错误的代码,以及注释掉的解决方法尝试。目标是基于站点地图创建“同级”页面的水平列表,当前页面的链接及其之前的链接的样式略有不同。
@If Not (SiteMap.CurrentNode Is Nothing OrElse SiteMap.CurrentNode.ParentNode Is Nothing OrElse SiteMap.CurrentNode.ParentNode.ChildNodes.Count = 0) Then
@<div id="contentPageMenu">
@For Each childNode As SiteMapNode In SiteMap.CurrentNode.ParentNode.ChildNodes
@* GENERATES AN EXTRA SPACE BETWEEN EVERY a TAG! *@
If childNode Is SiteMap.CurrentNode Then
@<a class="top_menu_button top_menu_button_selected" href="@childNode.Url">@Trim(childNode.Title)</a>
ElseIf childNode.NextSibling Is SiteMap.CurrentNode Then
@<a class="top_menu_button top_menu_button_before_selected" href="@childNode.Url">@Trim(childNode.Title)</a>
Else
@<a class="top_menu_button" href="@childNode.Url">@Trim(childNode.Title)</a>
End If
@*
STILL GENERATES EXTRA WHITESPACE!
If childNode Is SiteMap.CurrentNode Then @<a class="top_menu_button top_menu_button_selected" href="@childNode.Url">@Trim(childNode.Title)</a>ElseIf childNode.NextSibling Is SiteMap.CurrentNode Then @<a class="top_menu_button top_menu_button_pre_selected" href="@childNode.Url">@Trim(childNode.Title)</a>Else @<a class="top_menu_button" href="@childNode.Url">@Trim(childNode.Title)</a>End If
*@
@*
CAN'T PARSE a OR If STATEMENTS!
@<a class=@If childNode Is SiteMap.CurrentNode Then @<text>"top_menu_button top_menu_button_selected"</text> ElseIf childNode.NextSibling Is SiteMap.CurrentNode Then @<text>"top_menu_button top_menu_button_pre_selected"</text> Else @<text>"top_menu_button"</text> End If@<text> href="</text>@childNode.Url@<text>"></text>@Trim(childNode.Title)@</a>
*@
Next
</div>
End If
PS - 我知道我仍然需要处理 MVC 的“规范 URL”问题,但这超出了这个问题的范围。
这是问题的直观示例。箭头是位于右上角的背景图像。
以下是用于设置这些按钮样式的 CSS:
a.top_menu_button
{
background: url(../Images/top_menu_separator.gif) no-repeat right top #01376a;
color: #ffffff;
display: inline-block;
font-family: inherit;
font-size: 1.0em;
font-weight: bolder;
height: 20px;
margin: 0;
padding: 2px 15px 2px 10px;
position: relative;
text-decoration: none;
top: 0;
white-space: nowrap;
}
a.top_menu_button:hover
{
font-family: inherit;
font-style: italic;
}
a.top_menu_button_before_selected
{
background: url(../Images/top_menu_separator_before_selected.gif) no-repeat right top #01376a;
}
a.top_menu_button_selected
{
background: url(../Images/top_menu_separator_after_selected.gif) no-repeat right top #b9aa64;
color: #01376a;
font-family: inherit;
font-style: italic;
}
I am getting extra whitespace characters rendered in my horizontal list of anchor tags below. This is causing a major CSS styling issue for me.
I know this question has been addressed as far as a C# workaround is concerned (asp.net mvc razor extra space), but can anyone help me with a VB workaround? I don't have the advantage of curly braces to eliminate all whitespace in a single-line If condition.
I have managed to work around it for now by writing an HtmlHelper extension method, but this dynamic code is only used in one place (the master layout page). It seems to me that this should really be done in the .vbhtml page. Any arguments against this opinion are also welcome.
Here is the erroneous code, along with commented-out attempts at workarounds. The goal is to create a horizontal list of "sibling" pages based on a site map, with slightly different styles for the current page's link and the link just before it.
@If Not (SiteMap.CurrentNode Is Nothing OrElse SiteMap.CurrentNode.ParentNode Is Nothing OrElse SiteMap.CurrentNode.ParentNode.ChildNodes.Count = 0) Then
@<div id="contentPageMenu">
@For Each childNode As SiteMapNode In SiteMap.CurrentNode.ParentNode.ChildNodes
@* GENERATES AN EXTRA SPACE BETWEEN EVERY a TAG! *@
If childNode Is SiteMap.CurrentNode Then
@<a class="top_menu_button top_menu_button_selected" href="@childNode.Url">@Trim(childNode.Title)</a>
ElseIf childNode.NextSibling Is SiteMap.CurrentNode Then
@<a class="top_menu_button top_menu_button_before_selected" href="@childNode.Url">@Trim(childNode.Title)</a>
Else
@<a class="top_menu_button" href="@childNode.Url">@Trim(childNode.Title)</a>
End If
@*
STILL GENERATES EXTRA WHITESPACE!
If childNode Is SiteMap.CurrentNode Then @<a class="top_menu_button top_menu_button_selected" href="@childNode.Url">@Trim(childNode.Title)</a>ElseIf childNode.NextSibling Is SiteMap.CurrentNode Then @<a class="top_menu_button top_menu_button_pre_selected" href="@childNode.Url">@Trim(childNode.Title)</a>Else @<a class="top_menu_button" href="@childNode.Url">@Trim(childNode.Title)</a>End If
*@
@*
CAN'T PARSE a OR If STATEMENTS!
@<a class=@If childNode Is SiteMap.CurrentNode Then @<text>"top_menu_button top_menu_button_selected"</text> ElseIf childNode.NextSibling Is SiteMap.CurrentNode Then @<text>"top_menu_button top_menu_button_pre_selected"</text> Else @<text>"top_menu_button"</text> End If@<text> href="</text>@childNode.Url@<text>"></text>@Trim(childNode.Title)@</a>
*@
Next
</div>
End If
P.S. - I know I still need to handle the "canonical URLs" issue for MVC, but that is outside the scope of this question.
Here is a visual example of the problem. The arrows are top-right positioned background images.
Here is the CSS used to style these buttons:
a.top_menu_button
{
background: url(../Images/top_menu_separator.gif) no-repeat right top #01376a;
color: #ffffff;
display: inline-block;
font-family: inherit;
font-size: 1.0em;
font-weight: bolder;
height: 20px;
margin: 0;
padding: 2px 15px 2px 10px;
position: relative;
text-decoration: none;
top: 0;
white-space: nowrap;
}
a.top_menu_button:hover
{
font-family: inherit;
font-style: italic;
}
a.top_menu_button_before_selected
{
background: url(../Images/top_menu_separator_before_selected.gif) no-repeat right top #01376a;
}
a.top_menu_button_selected
{
background: url(../Images/top_menu_separator_after_selected.gif) no-repeat right top #b9aa64;
color: #01376a;
font-family: inherit;
font-style: italic;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
尝试使用 @Code/@End Code 块和 @: 转义符。此外,如果您为使用 If/Then/Else 逻辑确定的事物创建一个变量,它可能会更具可读性。
顺便说一句,这是 HTML - 空格不重要。您确定这确实给您带来了问题吗?
-- 编辑 --
现在我看到了你的 CSS,问题就清楚了。您正在使用
inline-block
,这意味着空白在渲染中很重要,这就是您所发现的。您可以通过多种方式解决此问题,可以通过 CSS,也可以不在 Razor 中。float: left;
添加到您的a.top_menu_button
类中,这将解决该问题。或者...并为其指定样式
style="word-spacing: -1em"
。然后,将word-spacing: 0;
添加到您的a.top_menu_button
类中,这也将解决问题。尝试谷歌搜索“显示内联块空白”,您会得到多次有关问题和样式解决方法的点击。 这个 和 这个两者都清晰简洁地描述了问题以及解决问题的一些方法。
Try using @Code/@End Code blocks with the @: escape. Also, it might be more readable if you make a variable for the thing that you're using If/Then/Else logic to determine.
By the way, this is HTML - whitespace shouldn't matter. Are you sure that's really causing you a problem?
-- EDIT --
Now that I see your CSS, the problem is clear. You're using
inline-block
, which means that whitespace is significant in rendering, which is what you're finding. You can fix this a couple of ways, both via CSS and not in your Razor.float: left;
to youra.top_menu_button
class and that will fix the problem. Or...<div id="contentPageMenu">
and give it a stylestyle="word-spacing: -1em"
. Then, addword-spacing: 0;
to youra.top_menu_button
class and that will fix the problem as well.Try Googling "display inline block whitespace" and you'll get multiple hits talking about the problem and styling workarounds. This one and this one both give a clear and concise description of the problem and some ways around it.