skip to Main Content

I am creating a Vazor (VB.NET Razor) using XML literals supported in VB.NET. I generate a string containging Html code but it needs further processing to resolve paths, handle asp- attributes, do ant encryption or authentication … etc. All this work is already done in Razor, so I don’t want to reinvent the wheel. I want to know the part of the Razor doing this to deliver my HTML code to and get the work done.

I create a Class for each view, that implements IVazor Interface. The vbxml code is written in teh Vazor method. This is how it looks like:

    Public Function Vazor() As XElement Implements IVazor.Vazor
        ViewBag.Title = "Vazor Sample"
        Return _
 <p>
     <h3> Browse Students</h3>
     <p>Select from <%= students.Count() %> students:</p>
     <ul>
         <%= (Iterator Function()
                  For Each std In students
                      Yield <li><%= std.Name %></li>
                  Next
              End Function)() %>
     </ul>
     <script>
        var x = 5;
        document.writeln("students count = <%= students.Count() %>");
    </script>
 </p>

    End Function

I made a proof of concept here: https://github.com/VBAndCs/VB.NET-Razor
Note: I changed the VBRazor to Vazor but didn’t upload this yet.

I want to complete this work, but I need help. My Vazor delivers a string containing HTML code without any C# ot VB code, so it differs from Razor in three things:
1- no need to locate any chtml file.
2- no need to combine view parts (layout, sections, etc) because mu view classes take care of that (I didn’t complete this yet. I only cobine the layout for now)
3- no need co compile the View or evaluate any thing.

So, I deliver html code like this:

<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewBag.Title - WebApplication1</title>
    <environment include="Development">
      <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.css" />
    </environment>
    <environment exclude="Development">
      <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/css/bootstrap.min.css" asp-fallback-href="~/lib/bootstrap/dist/css/bootstrap.min.css" asp-fallback-test-class="sr-only" asp-fallback-test-property="position" asp-fallback-test-value="absolute" crossorigin="anonymous" integrity="sha256-eSi1q2PG6J7g7ib17yAaWMcrr5GrtohYChqibrV7PBE=" />
    </environment>
    <link rel="stylesheet" href="~/css/site.css" />
  </head>
  <body>
    <header>
      <nav Class="navbar navbar-expand-sm navbar-toggleable-sm navbar-light bg-white border-bottom box-shadow mb-3">
        <div Class="container">
          <a Class="navbar-brand" asp-area="" asp-controller="Home" asp-action="Index">WebApplication1</a>
          <button Class="navbar-toggler" type="button" data-toggle="collapse" data-target=".navbar-collapse" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
            <span Class="navbar-toggler-icon"></span>
          </button>
          <div Class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
            <ul Class="navbar-nav flex-grow-1">
              <li Class="nav-item">
                <a Class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
              </li>
              <li Class="nav-item">
                <a Class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
              </li>
            </ul>
          </div>
        </div>
      </nav>
    </header>
    <div Class="container">
      <partial name="_CookieConsentPartial" />
      <main role="main" class="pb-3">
        <p>
          <h3> Browse Students</h3>
          <p>Select from 3 students:</p>
          <ul>
            <li>Adam</li>
            <li>Mark</li>
            <li>Tom</li>
          </ul>
          <script>
        var x = 5;
        document.writeln("students count = 3");
    </script>
        </p>
      </main>
    </div>
    <footer Class="border-top footer text-muted">
      <div Class="container">
            copy; 2019 - WebApplication1 - <a asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a></div>
    </footer>
    <environment include="Development">
      <script src="~/lib/jquery/dist/jquery.js"></script>
      <script src="~/lib/bootstrap/dist/js/bootstrap.bundle.js"></script>
    </environment>
    <environment exclude="Development">
      <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js" asp-fallback-src="~/lib/jquery/dist/jquery.min.js" asp-fallback-test="window.jQuery" crossorigin="anonymous" integrity="sha256-FgpCb/KJQlLNfOu91ta32o/NMZxltwRo8QtmkMRdAu8="></script>
      <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.1.3/js/bootstrap.bundle.min.js" asp-fallback-src="~/lib/bootstrap/dist/js/bootstrap.bundle.min.js" asp-fallback-test="window.jQuery, window.jQuery.fn,  window.jQuery.fn.modal" crossorigin="anonymous" integrity="sha256-E/V4cWE4qvAeO5MOhjtGtqDzPndRO1LBk8lJ/PR7CA4="></script>
    </environment>
    <script src="~/js/site.js" asp-append-version="true"></script>
        @RenderSection("Scripts", false)
    </body>
</html>

This Html code needs further processing to resolve paths, handle asp- attributes, do ant encryption or authentication … etc. All this work is already done in Razor, so I don’t want to reinvent the wheel. I want to know the part of the Razor doing this to deliver my HTML code to and get the work done. I spend days looking at the Razor code but didn’t get what I want, so a little help is appreciated here.
And who are interested, they can participate in the discussion here:
https://github.com/dotnet/vblang/issues/397
Thanks.

2

Answers


  1. Chosen as BEST ANSWER

    I found a perfgect easy solution, by using VirtualPathProvider. But it disapeared in ASP.NET Core! I found an alternative with IFileProvider.. More details here: https://www.mikesdotnetting.com/article/301/loading-asp-net-core-mvc-views-from-a-database-or-other-location but when I tried to register my virual file provider with this code:

            services.Configure(Of MvcRazorRuntimeCompilationOptions)(
                       Sub(options) options.FileProviders.Add(New Vazor.VazorViewProvider())
               )
    
    

    I found that FileProviders is no longer a member of RazorViewEngineOptions!

    In ASP.NET Core 3.0, it is done using MvcRazorRuntimeCompilationOptions (needs to reference Microsoft.AspNetCore.Mvc.Razor.RuntimeCompilation:

    services.Configure(Of MvcRazorRuntimeCompilationOptions)(
      Sub(options) options.FileProviders.Add(New Vazor.VazorViewProvider())
    )
    

    And here is the implementation of this idea: Vazor 1.0 up and running: https://github.com/VBAndCs/Vazor


  2. Vazor is ready now. Start using it today to create wep pages for ASP.NEt Core 3.1 in VB.NET:
    1. Install Vazor project templates
    2. Install Html5 completion provider VS extension
    3. Use the instructions in the ReadMe file
    4. Use eShopOnWeb.VB as a guide app.
    5. Have fun 🙂

    This is the complete story behind Vazor.
    And this is a short version published in Visual studio Magazine article.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search