简体   繁体   中英

ASP.NET MVC application, <text> tag

In an ASP.NET MVC application I am working on, <text> tags are being used in a .cshtml file.

Example -

<text>some text</text>

What functionality do they provide? I can not find any reference to them on the interweb :)

Thanks!

The are kinda the opposite of the @ tags...

Standard in views you are in "HTML-mode", you can then use a Razor-block like this:

@{
  //Razor code
}

Razor will detect HTML-tags in a razor block and render that but sometimes you just need to show the literal text. That's where the tag comes in... It switches back to HTML-mode without using an actual HTML-tag...

So some text will render that exact text (WITHOUT the tag>) in your view: Convaluted example, but this code:

<div>
you have
@{
 if(numItems == 0)
 {
   <text>no</text>
 }
 else
 {
   @numItems
 }


}
items
</div>

Will render "You have no items" or "you have 5 items" for example...

This element for identifying content explicitly. You can read information about this here: http://weblogs.asp.net/scottgu/archive/2010/12/15/asp-net-mvc-3-razor-s-and-lt-text-gt-syntax.aspx

The <text> explicitly tells the Razor view engine that this is text and not code. I use it when the renderer struggles with the page or if the html needs to be tightly controlled.

For example

<div id="content" @if (ViewData["PageLayout"] != null){
<text>class="@ViewData["PageLayout"].ToString()</text><text>"</text>
}>

See this post from Scott Gu.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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