简体   繁体   English

如何防止空白被修剪?

[英]How to prevent whitespace from being trimmed?

Please take a look at the following ASP.NET code: 请查看以下ASP.NET代码:

<%@ Page Language="C#" %>
<!doctype html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <p><%Response.Write("This is sentence 1.");%> <%Response.Write("This is sentence 2.");%></p>
    </body>
</html>

I expected it to build a short paragraph by joining two strings together, with a white space between them (please note the white char between <%Response.Write("This is sentence 1.");%> and <%Response.Write("This is sentence 2.");%> ). 我希望它通过将两个字符串连接在一起来构建一个短段,它们之间有一个空格(请注意<%Response.Write("This is sentence 1.");%>之间的白色字符<%Response.Write("This is sentence 1.");%><%Response.Write("This is sentence 2.");%> )。 However, the output HTML I get from IIS 7.5 is: 但是,我从IIS 7.5获得的输出HTML是:

<!doctype html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <p>This is sentence 1.This is sentence 2.</p>
    </body>
</html>

Which contains no white space between both sentences. 两个句子之间不包含空格。 Interestingly, if I place the white space inside the second sentence: 有趣的是,如果我将空格放在第二句中:

<%@ Page Language="C#" %>
<!doctype html>
<html>
    <head>
        <title></title>
    </head>
    <body>
        <p><%Response.Write("This is sentence 1.");%><%Response.Write(" This is sentence 2.");%></p>
    </body>
</html>

Then it is carried on to the HTML. 然后它继续到HTML。 But I would prefer the white spaces to be in the code building the composition, not in the data on which it works, since I do not know, by the time I write the individual sentences, which ones will go into the paragraph or in what order. 但是我希望白色空间在构建组合的代码中,而不是在它工作的数据中,因为我不知道,当我写单个句子时,哪些将进入段落或什么订购。

Is this the expected behaviour, or am I doing something wrong? 这是预期的行为,还是我做错了什么?

UPDATE: 更新:

VDWWD points out an interesting remark; VDWWD指出了一个有趣的评论; if I use <%="..."%> instead of <%Response.Write("...");%> the whitespace is indeed carried on to the HTML. 如果我使用<%="..."%>而不是<%Response.Write("...");%>确实将空格继承到HTML。 But that makes me scratch my head even more, because this works on my simplified test case posted above, but not on my actual use case which looks more like this: 但这让我更加抓狂了,因为这适用于上面发布的简化测试用例,但不是我的实际用例,看起来更像是这样:

...
<p><%=TextoWeb("Ponencias", "QuieresSubirTuPonencia?")%><%
var InicioPonencias = Sesión.ElementoTimelinePorNombre("INICIO PONENCIAS");
if (DateTime.Today < InicioPonencias.Fecha) {
    %> <%=TextoWeb("Ponencias", "TextoAntesAperturaPonencias", InicioPonencias.Fecha.ToLongDateHtml(Sesión.Cultura))%><%
}
%></p>
...

Please excuse the Spanish and the non-standard extensions. 请原谅西班牙语和非标准扩展名。 Function TextoWeb retrieves some localised text by category and name according to the language of the page being built, Sesión.ElementoTimelinePorNombre retrieves some timeline item by name and .ToLongDateHtml(System.Globalization.CultureInfo) does some language-specific high level formatting of the dates to add things like ordinal indicators. 函数TextoWeb根据正在构建的页面的语言按类别和名称检索一些本地化文本, Sesión.ElementoTimelinePorNombre按名称检索一些时间轴项目.ToLongDateHtml(System.Globalization.CultureInfo)执行某些语言特定的日期高级格式化添加序数指标之类的东西。 The purpose of this specific piece of code is adding a sentence to an existing paragraph, but only if the current date is earlier than a certain date. 此特定代码段的目的是向现有段落添加句子,但仅限于当前日期早于特定日期。

The thing is that I am using <%=(...)%> instead of <%Response.Write(...);%> but the white space is not being carried on to the HTML. 问题是我使用<%=(...)%>而不是<%Response.Write(...);%>但是没有将空格带到HTML上。

You can use a HTML entity which is &#32; 您可以使用HTML实体&#32; for a regular space, or the more memorable &nbsp; 对于一个普通的空间,或者更难忘的&nbsp; for a non-breaking space if that is desired. 如果需要,可以获得不间断的空间。

I don't know the exact mechanism behind the stripping, but most likely it has something to do with the order in which cshtml is compiled. 我不知道剥离背后的确切机制,但很可能它与编译cshtml的顺序有关。 Stripping the whitespace from an empty tag seen as <p> </p> by the engine before being populated would be expected behaviour. 在填充之前,引擎从空标记中剥离空白标记( <p> </p>将是预期的行为。

Do not use Response.Write. 不要使用Response.Write。 Then the space is there. 然后空间就在那里。

<%= "This is sentence 1." %> <%= "This is sentence 2." %>

Update 更新

You probably don't get a space because you have split the <% %> between inline code. 您可能没有获得空格,因为您在内联代码之间拆分了<% %> The compiler makes it a single line. 编译器使它成为一行。 Try to do the if statements in code behind for much cleaner aspx. 尝试在代码后面执行if语句以获得更清晰的aspx。 Or you a ternary opertator, that way there is also a space. 或者你是一个三元运营商,那样也有一个空间。

<p><%=TextoWeb("Ponencias", "QuieresSubirTuPonencia?YYY")%> <%= DateTime.Today < DateTime.Now ? TextoWeb("xxxPonencias", "QuieresSubirTuPonencia?YYY") : "" %></p>

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

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