How can I isolate Razor species?

Is there a way to publish Razor syntax and (custom) helpers for people, but tell me ... don't let them create blocks of code or restrict them to only using helpers and not give them the power to execute pure C # code in views?

Any ideas and pointers to such solutions are welcome!

update: // I would like to give users the opportunity to write their own HTML code and access only the list of html helpers. Mostly the default and the ones I create.

For example, I do not want them to be able to execute the code inside the block @{ //code }, and also there usingand @model(not sure about this) only have access to@Html.* @if else for foreach

or better yet, give them access only to specific namespaces (this is just a tho thought)

Update: // After some testing, I found out what RazorEngine does as close as possible to what I'm trying to do: run the views in an isolated environment and add access to certain namespaces.

+5
source share
4 answers

There is a project called RazorEngine built on Microsoft Razor that allows you to parse this syntax without being in the context of returning an MVC view. Here's how it is used:

 string template = "Hello @Model.Name! Welcome to Razor!";
 string result = Razor.Parse(template, new { Name = "World" });

You can also specify a custom template base that should allow you to define only those Html helpers that you want to provide to your users:

 Razor.SetTemplateBase(typeof(HtmlTemplateBase<>));

 string template = 
  @"<html>
      <head>
        <title>Hello @Model.Name</title>
      </head>
      <body>
        Email: @Html.TextBoxFor(m => m.Email)
      </body>
    </html>";

  var model = new PageModel { Name = "World", Email = "someone@somewhere.com" };
  string result = Razor.Parse(template, model);
+2
source

. . , . , , DotLiquid.

+4

.

( ), ( # vb.net). , ().

, ( ) ( ).

, , .

  • .
  • , var r = new Random();
  • ,
  • "" , .

, . , AviatrixTemplate, . , - , ( ). AviatrixTemplate , .

PS: , . , . Activator.CreateInstance.

@for (var r = new Random(); r != null; r = null)
{
    @r.NextDouble()
}

.

+2

, , , ?

, CSharpCodeProvider, RazorTemplateEngine System.CodeCom.Compiler.

:

CSharpCodeProvider: http://support.microsoft.com/kb/304655

RazorTemplateEngine: http://msdn.microsoft.com/en-us/library/system.web.razor.razortemplateengine(v=vs.111).aspx

0
source

All Articles