从 sql 渲染 html - webmatrix [英] Render html from sql - webmatrix

查看:47
本文介绍了从 sql 渲染 html - webmatrix的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有 webmatrix 的 cshtml 页面,我正在尝试呈现存储在我的数据库中的 html,但输出类似于 <b>Lorem ipsum dolor sat amet, consectetur adipiscing elit.</b> 而不是 Lorem ipsum dolor sat amet, consectetur adipiscing elit.(这只是一个解释正在发生的事情的例子)

I'm using cshtml pages with webmatrix, i'm trying to render the html that is stored in my db, but the output is like <b>Lorem ipsum dolor sit amet, consectetur adipiscing elit. </b> instead of Lorem ipsum dolor sit amet, consectetur adipiscing elit. (This is just an example to explain what is happening)

我以 ntext 数据类型存储.

I'm storing in ntext datatype.

这是我的代码.

  @{
   var db = Database.Open("myDB");
   var selectQueryString = "SELECT * FROM noticias ORDER BY id";
   }
 <!DOCTYPE html>
   <html lang="en">
      <head>
         <meta charset="utf-8" />
         <title></title>
          <style>
           p {color: #f00;}
         </style>
     </head>
     <body>

        @foreach(var row in db.Query(selectQueryString)){

               <p>@row.header</p>
               <p>@row.description</p>

       }

    </body>
  </html>

推荐答案

带有@"的语法会自动应用 HTML 敏感字符的编码.如果您的变量(或方法调用等)返回一个包含 HTML 标记的字符串,并且您希望浏览器呈现该标记,请将字符串包装在 MvcHtmlString 中.

The syntax with '@' automatically applies encoding of HTML sensitive characters. If your variables (or method call, etc.) returns a string that contains HTML markup and you want the browser to render that markup, wrap the string in a MvcHtmlString.

其中任何一个都可以:

@Html.Raw(row.header)
@(new MvcHtmlString(row.header))
@MvcHtmlString.Create(row.header)

请注意,这会将浏览器暴露给值中包含的任何 HTML 标记.仅当验证数据仅包含安全标记、无脚本等时才应使用此方法.

Note that this will expose the browser to ANY HTML markup contained in the values. This method should only be used if the data is verified to only contain safe markup, no scripts, etc.

这篇关于从 sql 渲染 html - webmatrix的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆