简体   繁体   English

在 Razor 页面中将文本显示为 html

[英]Display text as html in Razor page

I've just started using Blazor and Razor pages.. it's a lot better than my old plain html pages I was building.我刚刚开始使用 Blazor 和 Razor 页面。它比我正在构建的旧的普通 html 页面要好得多。 But I'm having a problem converting my strings of html I build into actual HTML.但是我在将我构建的 html 字符串转换为实际的 HTML 时遇到问题。 Now, this works if I copy and paste my string and save that as an html page.现在,如果我复制并粘贴我的字符串并将其保存为 html 页面,这将有效。 It displays as html no problem.它显示为 html 没问题。 However, when I try to get my Razor page to display it, it displays as plain text.但是,当我尝试让我的 Razor 页面显示它时,它显示为纯文本。 Even trying the trick:甚至尝试技巧:

<div>
    @(new HtmlString(htmlString))
</div>

displays plain text.显示纯文本。 This is going to be painful as it's kind of long, but here is the string I'm trying to convert:这会很痛苦,因为它有点长,但这是我要转换的字符串:

"<pre><br><b>STATION INFO</b><br><br>            Extension: 34263                        Lock Messages? n        BCC: 0 <br>                 Type: 9630                         Security Code: 12345     TN: 1<br>                 Port: S17746                     Coverage Path 1: 132      COR: 44<br>                 Name: Lastname, Firstname                  Coverage Path 2:          COS: 1<br><br><b>STATION OPTIONS</b><br>                                                       Time of Day Lock Table: <br>                 Loss Group: 19                  Personalized Ringing Pattern: 1<br>                                                             Message Lamp Ext: 34263<br>               Speakerphone: 2-way                        Mute Button Enabled? y<br>           Display Language: english                         Expansion Module? <br>    Survivable GK Node Name: <br>             Survivable COR: internal                       Media Complex Ext: <br>      Survivable Trunk Dest?                                     IP SoftPhone? y<br><br>                                                           IP Video Softphone? n<br><br>                                                          Customizable Labels? y<br><br><br> <b>FEATURE OPTIONS</b><br><br>            LWC Reception: spe                 Auto Select Any Idle Appearance? n<br>           LWC Activation? y                            Coverage Msg Retrieval? y<br>   LWC Log External Calls? n                                       Auto Answer: none<br>              CDR Privacy? n                                  Data Restriction? n<br>    Redirect Notification? y                        Idle Appearance Preference? n<br>  Per Button Ring Control? n                      Bridged Idle Line Preference? n<br>    Bridged Call Alerting? n                          Restrict Last Appearance? y<br>   Active Station Ringing: continuous<br>                                                             EMU Login Allowed? n<br>         H.320 Conversion? n             Per Station CPN - Send Calling Number? y<br>        Service Link Mode: as-needed                      EC500 State: unknown<br>          Multimedia Mode: enhanced                    Audible Message Waiting? n<br>     MWI Served User Type: sip-adjunct              Display Client Redirection? n</pre><br>"

it's being built by my method that returns a string.它是由我的返回字符串的方法构建的。 For fun, here is my method:为了好玩,这是我的方法:

    public static string CreateStationString(List<StarfishStation> html)
    {
        var myHTML1 =
                    "<pre>" + "<br>" +
                "<b>STATION INFO</b>" + "<br>" +
                "" + "<br>" +
                "            Extension: " + html[0].DeftyExtension.PadRight(29, ' ') + "Lock Messages? " + html[0].DeftyMessagelock.PadRight(9, ' ') + "BCC: 0 <br>" +
                "                 Type: " + html[0].DeftyDisplaysettype.PadRight(29, ' ') + "Security Code: " + html[0].DeftySecuritycode.PadRight(10, ' ') + "TN: " + html[0].DeftyTenantpartitionnumber + "<br>" +
                "                 Port: " + html[0].DeftyPort.PadRight(27, ' ') + "Coverage Path 1: " + html[0].DeftyCoveragepath.PadRight(9, ' ') + "COR: " + html[0].DeftyCor + "<br>" +
                "                 Name: " + html[0].DeftyName.PadRight(27, ' ') + "Coverage Path 2: " + html[0].DeftyCoverage2path.PadRight(9, ' ') + "COS: " + html[0].DeftyCos + "<br><br>" +
                "<b>STATION OPTIONS</b>" + "<br>" +
                "                                                       Time of Day Lock Table: <br>" +
                "                 Loss Group: " + html[0].DeftyLossgroup + "                  Personalized Ringing Pattern: " + html[0].DeftyPersonalizedringpattern + "<br>" +
                "                                                             Message Lamp Ext: " + html[0].DeftyMessagewaitlamplextension + "<br>" +
                "               Speakerphone: " + html[0].DeftySpeakerphone + "                        Mute Button Enabled? " + html[0].DeftyMutebutton + "<br>" +
                "           Display Language: " + html[0].DeftyLanguage.PadRight(32, ' ') + "Expansion Module? " + html[0].DeftyExpansionmodule + "<br>" +
                "    Survivable GK Node Name: <br>" +
                "             Survivable COR: internal                       Media Complex Ext: <br>" +
                "      Survivable Trunk Dest?                                     IP SoftPhone? " + html[0].DeftyIpsoftphone + "<br>" +
                "" + "<br>" +
                "                                                           IP Video Softphone? n<br>" +
                "<br>" +
                "                                                          Customizable Labels? y<br><br>" +
                "<br>" +
                " <b>FEATURE OPTIONS</b><br><br>" +
                "            LWC Reception: " + html[0].DeftyLwcreception.PadRight(20, ' ') + "Auto Select Any Idle Appearance? " + html[0].DeftyIdleappearancepreference + "<br>" +
                "           LWC Activation? " + html[0].DeftyLwcactivation.PadRight(29, ' ') + "Coverage Msg Retrieval? " + html[0].DeftyCoveragemessageretrieval + "<br>" +
                "   LWC Log External Calls? " + html[0].DeftyLwclogexterncall.PadRight(40, ' ') + "Auto Answer: " + html[0].DeftyAutoanswer + "<br>" +
                "              CDR Privacy? " + html[0].DeftyCdrprivacy.PadRight(35, ' ') + "Data Restriction? " + html[0].DeftyDatarestriction + "<br>" +
                "    Redirect Notification? " + html[0].DeftyRedirectnotification.PadRight(25, ' ') + "Idle Appearance Preference? " + html[0].DeftyIdleappearancepreference + "<br>" +
                "  Per Button Ring Control? " + html[0].DeftyPerbuttonnringcontrol.PadRight(23, ' ') + "Bridged Idle Line Preference? n" + "<br>" +
                "    Bridged Call Alerting? " + html[0].DeftyBridgecallalerting.PadRight(27, ' ') + "Restrict Last Appearance? " + html[0].DeftyRestrictlastappearance + "<br>" +
                "   Active Station Ringing: " + html[0].DeftyActivestationringing + "<br>" +
                "                                                             EMU Login Allowed? " + html[0].DeftyEMULoginAllowed + "<br>" +
                "         H.320 Conversion? " + html[0].DeftyH320conv.PadRight(14, ' ') + "Per Station CPN - Send Calling Number? " + html[0].DeftyCpnrestriction + "<br>" +
                "        Service Link Mode: " + html[0].DeftyServicelinkmode.PadRight(31, ' ') + "EC500 State: unknown<br>" + //check ec500
                "          Multimedia Mode: " + html[0].DeftyMultimediamode.PadRight(28, ' ') + "Audible Message Waiting? " + html[0].DeftyAudiblemessagewaiting + "<br>" +
                "     MWI Served User Type: " + html[0].DeftyMessagewaitindicatortype.PadRight(25, ' ') + "Display Client Redirection? n</pre><br>";

        return myHTML1;
    }

When I look at the frame source, here's what I see:当我查看帧源时,我看到的是:

<div>
    &lt;pre&gt;&lt;br&gt;&lt;b&gt;STATION INFO&lt;/b&gt;&lt;br&gt;&lt;br&gt;            Extension: 34263                        Lock Messages? n        BCC: 0 &lt;br&gt;                 Type: 9630                         Security Code: 12345     TN: 1&lt;br&gt;                 Port: S17746                     Coverage Path 1: 132      COR: 44&lt;br&gt;                 Name: Lastname Firstname                  Coverage Path 2:          COS: 1&lt;br&gt;&lt;br&gt;&lt;b&gt;STATION OPTIONS&lt;/b&gt;&lt;br&gt;                                                       Time of Day Lock Table: &lt;br&gt;                 Loss Group: 19                  Personalized Ringing Pattern: 1&lt;br&gt;                                                             Message Lamp Ext: 34263&lt;br&gt;               Speakerphone: 2-way                        Mute Button Enabled? y&lt;br&gt;           Display Language: english                         Expansion Module? &lt;br&gt;    Survivable GK Node Name: &lt;br&gt;             Survivable COR: internal                       Media Complex Ext: &lt;br&gt;      Survivable Trunk Dest?                                     IP SoftPhone? y&lt;br&gt;&lt;br&gt;                                                           IP Video Softphone? n&lt;br&gt;&lt;br&gt;                                                          Customizable Labels? y&lt;br&gt;&lt;br&gt;&lt;br&gt; &lt;b&gt;FEATURE OPTIONS&lt;/b&gt;&lt;br&gt;&lt;br&gt;            LWC Reception: spe                 Auto Select Any Idle Appearance? n&lt;br&gt;           LWC Activation? y                            Coverage Msg Retrieval? y&lt;br&gt;   LWC Log External Calls? n                                       Auto Answer: none&lt;br&gt;              CDR Privacy? n                                  Data Restriction? n&lt;br&gt;    Redirect Notification? y                        Idle Appearance Preference? n&lt;br&gt;  Per Button Ring Control? n                      Bridged Idle Line Preference? n&lt;br&gt;    Bridged Call Alerting? n                          Restrict Last Appearance? y&lt;br&gt;   Active Station Ringing: continuous&lt;br&gt;                                                             EMU Login Allowed? n&lt;br&gt;         H.320 Conversion? n             Per Station CPN - Send Calling Number? y&lt;br&gt;        Service Link Mode: as-needed                      EC500 State: unknown&lt;br&gt;          Multimedia Mode: enhanced                    Audible Message Waiting? n&lt;br&gt;     MWI Served User Type: sip-adjunct              Display Client Redirection? n&lt;/pre&gt;&lt;br&gt;
</div>

Sorry this is so long.. but can anyone please help me out?对不起,这太长了..但是有人可以帮我吗?

Thanks!谢谢!

Dave M.戴夫·M。

Wow ok.. so stumbled upon the answer:哇,好吧..偶然发现了答案:

<div>
    @((MarkupString)htmlString)
</div>

instead of:代替:

<div>
    @(new HtmlString(htmlString))
</div>

found the answer here: Convert plain text with html tags to html string and render it in Blazor在这里找到答案: 将带有 html 标签的纯文本转换为 html 字符串并在 Blazor 中呈现

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

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