简体   繁体   English

这个ASP.NET功能在哪里记录? <%=字符串格式,params object [] args%>

[英]Where is this ASP.NET feature documented? <%= string format, params object[] args %>

Apparently it is possible to write formatted output using the <%= %> construct ( render block ) in ASP.NET web forms pages and views. 显然,可以使用ASP.NET Web窗体页面和视图中的<%= %>构造( 渲染块 )编写格式化输出。

<%= "{0} is {1}", "Foo", 42 %>

This will render "Foo is 42". 这将使“Foo为42”。 As far as I know the ASP.NET parser translates <%= %> into a call to HttpResponse.Write(string) . 据我所知,ASP.NET解析器将<%= %>转换为对HttpResponse.Write(string)的调用。 Obviously in the code above, there is no one-to-one translation, because the number of arguments don't match (assuming the , in the expression above separates arguments). 显然,在上面的代码中,没有一个对一翻译,因为参数的数目不匹配(假设,在上面的表达式中分离参数)。

Now I have seen that the class TextWriter has a Write(string, object[]) method. 现在我已经看到TextWriter类有一个Write(string, object[])方法。

I have checked the output from the parser, and indeed it calls the TextWriter 's method that accepts a params object[] argument for formatting: 我检查了解析器的输出,实际上它调用TextWriter的方法接受params object[]参数进行格式化:

private void @__Renderform1(System.Web.UI.HtmlTextWriter @__w, System.Web.UI.Control parameterContainer) {
    // ...
    @__w.Write( "{0} is {1}", "Foo", 42 );

Is that behavior documented anywhere? 这种行为记录在哪里吗?

As far as I know the ASP.NET parser translates <%= %> into a call to HttpResponse.Write(string). 据我所知,ASP.NET解析器将<%=%>转换为对HttpResponse.Write(字符串)的调用。

Maybe the <%= "{0} is {1}", "Foo", 42 %> is translated to Response.Output.Write(string format, params object[] arg) , Output being of type TextWriter , which would be the explanation according to http://www.hanselman.com/blog/ASPNETResponseWriteAndResponseOutputWriteKnowTheDifference.aspx 也许<%= "{0} is {1}", "Foo", 42 %>被翻译为Response.Output.Write(string format, params object[] arg)Output类型为TextWriter ,这将是根据http://www.hanselman.com/blog/ASPNETResponseWriteAndResponseOutputWriteKnowTheDifference.aspx的解释

This is an <%= %> embedded code block and exists to maintain compatibility with Classic ASP. 这是一个<%= %> 嵌入式代码块 ,用于保持与Classic ASP的兼容性。

As you saw <%= "{0} is {1}", "Foo", 42 %> is equivalent to: 如您所见<%= "{0} is {1}", "Foo", 42 %>相当于:

string s = string.Format("{0} is {1}", "Foo", 42);
Response.Write(s);

That behavior is documented here : 这种行为记录在这里

Writes a formatted string that contains the text representation of an object array to the output stream, along with any pending tab spacing. 将包含对象数组的文本表示形式的格式化字符串写入输出流,以及任何挂起的制表符间距。 This method uses the same semantics as the String.Format method. 此方法使用与String.Format方法相同的语义。 (Overrides TextWriter.Write(String, Object[]).) (重写TextWriter.Write(String,Object [])。)

Here is where it's documented that the Code Render Block calls the Write method. 以下是代码渲染块调用Write方法的文档。

Finally, the syntax for embedded code blocks has been updated for .NET 4 as described here . 最后,如所描述的嵌入式代码块的语法已更新的.NET 4 这里

这很接近并且可能与http://msdn.microsoft.com/en-us/library/586y06yf.aspx相关,但它不是为什么<%=这样做的解释...

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

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