asp.net razor template engine output HTML in two ways _ practical tips

Source: Internet
Author: User

The examples in this article describe two ways to output HTML in the ASP.net razor template engine. Share to everyone for your reference, specific as follows:

All of the HTML in the razor is encoded automatically, so that we don't need to encode it manually (secure), but it is the HTML text that has been escaped when the HTML needs to be exported, as follows:

@{
  String thistest = "<span style=\" color: #f00; \ ">qubernet</span>";
}
@thisTest;

The text that is printed on the page is: <span style=\ "color: #f00; \" >qubernet</span> instead of the red font, to output the red font, there are two common ways:

1. Use the Html.raw in razor (recommended in this way):

@{
  String thistest = "<span style=\" color: #f00; \ ">qubernet</span>";
}
@Html. Raw (Thistest);

2. Use the Mvchtmlstring class to implement:

@{
  String thistest = "<span style=\" color: #f00; \ ">qubernet</span>";
  var thisresult = new mvchtmlstring (thistest);
}
@thisResult or @ (new htmlstring (thistest))

More interested in asp.net related content readers can view the site topics: "ASP.net optimization skills summary", "asp.net string operation skills Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", "asp.net Ajax Skills Summary topic and the ASP.net caching operation skills Summary.

I hope this article will help you to ASP.net program design.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.