Displaying Razor code without executing it.

Dean Everhart 1,536 Reputation points
2023-03-24T12:18:26.4966667+00:00

The code below, using the <pre></pre> tag using <> substitution displays the html code successfully on a view page (see below)

BUT...

it requires the Razor elements...

@RenderBody()

@await RenderSectionAsync("Scripts", required: false)

...to be commented out.

Question:

Is there any way to display the Razor elements within the code below on a .net page without executing the code?

<pre>
   &lt;!DOCTYPE html&gt;
      &lt;html lang="en"&gt;
   &lt;head&gt;
        &lt;meta charset="utf-8" /&gt;
        &lt;meta name="viewport" content="width=device-width, initial-scale=1.0" /&gt;
        &lt;title&gt;@ViewData["Title"] - Contoso University&lt;/title&gt;
        &lt;link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" /&gt;
        &lt;link rel="stylesheet" href="~/css/site.css" /&gt;
    &lt;/head&gt;
    &lt;body&gt;
        &lt;header&gt;
            &lt;nav class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3"&gt;
                &lt;div class="container"&gt;
                    &lt;a class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index"&gt;Contoso University&lt;/a&gt;
                    &lt;button class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent"
                            aria-expanded="false" aria-label="Toggle navigation"&gt;
                        &lt;span class="navbar-toggler-icon"&gt;&lt;/span&gt;
                    &lt;/button&gt;
                    &lt;div class="navbar-collapse collapse d-sm-inline-flex justify-content-between"&gt;
                        &lt;ul class="navbar-nav flex-grow-1"&gt;
                            &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index"&gt;Home&lt;/a&gt;
                            &lt;/li&gt;
                            &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="About"&gt;About&lt;/a&gt;
                            &lt;/li&gt;
                            &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link text-dark" asp-area="" asp-controller="Students" asp-action="Index"&gt;Students&lt;/a&gt;
                            &lt;/li&gt;
                            &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link text-dark" asp-area="" asp-controller="Courses" asp-action="Index"&gt;Courses&lt;/a&gt;
                            &lt;/li&gt;
                            &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link text-dark" asp-area="" asp-controller="Instructors" asp-action="Index"&gt;Instructors&lt;/a&gt;
                            &lt;/li&gt;
                            &lt;li class="nav-item"&gt;
                                &lt;a class="nav-link text-dark" asp-area="" asp-controller="Departments" asp-action="Index"&gt;Departments&lt;/a&gt;
                            &lt;/li&gt;
                        &lt;/ul&gt;
                    &lt;/div&gt;
                &lt;/div&gt;
            &lt;/nav&gt;
        &lt;/header&gt;
        &lt;div class="container"&gt;
            &lt;main role="main" class="pb-3"&gt;
@*@RenderBody()*@
            &lt;/main&gt;
        &lt;/div&gt;

        &lt;footer class="border-top footer text-muted"&gt;
            &lt;div class="container"&gt;
                &copy; 2020 - Contoso University - &lt;a asp-area="" asp-controller="Home" asp-action="Privacy"&gt;Privacy&lt;/a&gt;
            &lt;/div&gt;
        &lt;/footer&gt;
        &lt;script src="~/lib/jquery/dist/jquery.js"&gt;&lt;/script&gt;
        &lt;script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"&gt;&lt;/script&gt;
        &lt;script src="~/js/site.js" asp-append-version="true"&gt;&lt;/script&gt;
@*@await RenderSectionAsync("Scripts", required: false)*@
    &lt;/body&gt;
    &lt;/html&gt;
&lt;/pre&gt;
</pre>

Code above (with <> substitutions) displays...

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,553 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,481 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,291 Reputation points
    2023-03-24T13:32:16.08+00:00

    Rather than embedding the content in a Razor Page, place the content is a separate file in the wwwroot folder using the .html extension. Use an iFrame or object element to display the content.

    <object data="/Content/EncodedHtml.html" width="90%" height="500">
            Not supported
    </object>
    
    <iframe src="/Content/EncodedHtml.html" width="90%" height="500" title="Razor Markup">
      
    </iframe>
    
    0 comments No comments

0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.