简体   繁体   English

如何在MVC中的视图中显示剃刀和C#代码

[英]How to display razor and C# code in view in MVC

I am using MVC and razor for a style Guide. 我正在使用MVC和剃须刀作为样式指南。 I need to display the code in the view so others can use it on the site. 我需要在视图中显示代码,以便其他人可以在网站上使用它。 I need to display the code without the Razor helper executing the function. 我需要在没有Razor帮助程序执行功能的情况下显示代码。

I am trying to do this via a Razor helper. 我正在尝试通过剃刀助手来做到这一点。 I want this helper to render the button as well as display the exact MVC Razor helper code in the view (rather than have it display the outputted HTML from the helper). 我希望该帮助器呈现按钮并在视图中显示确切的MVC Razor帮助器代码(而不是让其显示从帮助器输出的HTML)。

@helper ShowCode() { 
        <div>
            @Html.TextBox("textBox")
        </div>
    }

Then I call the helper below and try and display the code: 然后,我致电下面的帮助程序,并尝试显示代码:

   // render the button
   @ShowCode()

        // display the button code
        <pre>
            <code>
                @ShowCode().ToString()
            </code>
        </pre>

However, this displays the following: 但是,将显示以下内容: 在此处输入图片说明

I need it to display the following code instead of the HTML: 我需要它来显示以下代码而不是HTML:

 <div>
    @Html.TextBox("textBox")
 </div>

How can I display the code within the Razor helper without it being outputted as html? 如何在Razor帮助器中显示代码而不将其输出为html? (I want to display exactly what is in the Razor helper). (我想确切显示Razor帮助器中的内容)。 Is this possible? 这可能吗?

You can't actually do it like that. 您实际上不能那样做。 Razor returns HTML there is no way to get actual Razor template code as string. Razor返回HTML,无法将实际的Razor模板代码获取为字符串。

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

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