На самом деле я не использую Razor из ASP.NET MVC, я использую автономную версию, найденную здесь
Я создал свой собственный HtmlHelper, как описано здесь
Я методом проб и ошибок определил, что атрибуты razor <text>
при вызове в контексте метода создают объект Func<Object, TemplateWriter>
.
Сигнатура метода помощника выглядит следующим образом:
public String IncludeOnce(Func<Object, TemplateWriter> text) {
//here I need to be able to render the text Func to a string so
//I can do some checks, and return it if it hasnt yet been included
//or return an empty string if it has
}
Я вызываю его в своем шаблоне, как:
@Html.IncludeOnce(
@<text>
<style type="text/css">
/* styles I only want on the page once, and not everytime the template
is rendered. Note: I need @Model to work here too*/
.something { top: @Model.Top }
</style>
</text>)
Как я могу получить его как строку?Кроме того, если я передам его другому шаблону, например:
public String IncludeOnce(Func<Object, TemplateWriter> text) {
return Razor.Parse("other.cshtml", new { Content = text(new Object()) })
}
, где other.cshtml
:
@Model.Content
, это работает.Что Razor.Parse знает, что я не?
Спасибо!