0

I am trying to develop a simple CMS using Blazor Server App (or Blazor Web App). I want users to define their own web pages' layout/template and contents online (in admin section). And, they will be stored in DB as string. Then, by calling user-defined routes, the content is fetched from DB and should be rendered runtime.

The output should be the same as if the user create Razor component and build the project (the template may have @code {} section).

I know that I can use RenderFragment, but it does not compile the code inside the markup.

For .cshtml files, it is possible implementing and registering custom Virtual FileProvider. But, it does not work for .razor files.

I am trying with Blazor Server App (or Blazor Web App in .NET 8).

Is it possible to build a Razor component runtime from a string variable?

Amir Pournasserian
  • 1,600
  • 5
  • 22
  • 46
  • No. Razor files are compiled at compile time into C# classes. You can use the `DynamicComponent` to instantiate existing components and define html content as `MarkupString`. A `RenderFragment` is a delegate, not a string. [Polite] Writing a CMS is far more complicated than saving a markup string to a database and having a render fragment render it. – MrC aka Shaun Curtis Aug 15 '23 at 08:55
  • It is possible to compile C# code runtime here is an example: https://stackoverflow.com/questions/60042367/dynamically-compile-and-run-code-using-net-core-3-0-for-scripting I'm looking for similar way for razor source. – Amir Pournasserian Aug 15 '23 at 13:22
  • You aren't asking how to compile code though. User supplied components and templates are possible, just not in the way you think. And you really, really DON'T want users to submit arbitrary code that could hack your site or attack others. That's why no CMS allows such a thing. – Panagiotis Kanavos Aug 24 '23 at 08:12
  • Hello have you tried the solution provided? Did you able to resolve the issue? Is there anything else that I can help you on this? – Md Farid Uddin Kiron Aug 29 '23 at 09:22

1 Answers1

0

Is it possible to build a Razor component runtime from a string variable?

Well, you can create dynamic content along with.cshtml file and you can create the file same as other file read/write operation using System.IO.File operation and it will create your .cshtml or .razor file as expected, Just like below:

    var cshtmlcontent = @"Any CS and HTML content you can pass";
    System.IO.File.WriteAllText(@"C:\Kiron\GitDemoApp\DotNet6MVCWebApp\wwwroot\Test.cshtml", cshtmlcontent);

However, you cannot run this file directly or it wouldn't act the way .cshtml or .razor component supposed to work, because, in asp.net core MVC .cshtml files are rendered based on your route definition at compile time, therefore, your newly created wouldn't have any designated route for that so it wouldn't compile and run the same way the .cshtml razor page does.

On the other hand, even you can write that using response.write or inside view folder and during that time, your application compiler will detected the changes and program would be restarted each time when you will create dynammic .cshtml file, that would lead to below pop-ups:

enter image description here

In addition to this, you can use ICompositeViewEngine to render dynamic file but still requried relevant controller and action or route to render view engine.

So, at runtime you cannot produce the expected result from .cshtml or .razor component.

Md Farid Uddin Kiron
  • 16,817
  • 3
  • 17
  • 43
  • Your solution works for .cshtml files, but not .razor files. Razor components are compiled during the build process. I am looking for a solution to compile Razor components runtime. – Amir Pournasserian Aug 29 '23 at 14:10
  • Thanks for your response. No, as described earlier, this wouldn't compile at runtime by design, So until now its hard to conclude to a runtme complilation solution. Because, according to architecture or design .cshtml and .razor dll process in compile time. – Md Farid Uddin Kiron Aug 30 '23 at 00:47