本文整理汇总了C#中System.Web.Razor.RazorTemplateEngine类的典型用法代码示例。如果您正苦于以下问题:C# RazorTemplateEngine类的具体用法?C# RazorTemplateEngine怎么用?C# RazorTemplateEngine使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
RazorTemplateEngine类属于System.Web.Razor命名空间,在下文中一共展示了RazorTemplateEngine类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: parse
public static ParsedTemplate parse(string sourceFilename, string cshtmlContent, string effectiveTemplateClassName, Type modelType)
{
var csCodeLanguage = new CSharpRazorCodeLanguage();
var templateHost = new RazorEngineHost(csCodeLanguage, () => new HtmlMarkupParser());
var concreteBaseClassType = getBaseClassTypeFromModel(modelType);
templateHost.DefaultBaseClass = concreteBaseClassType.FullName;
var templateEngine = new RazorTemplateEngine(templateHost);
var trimmedcshtmlContent = HeaderLines.trim(cshtmlContent);
GeneratorResults res;
using (var input = new StringReader(trimmedcshtmlContent))
{
res = templateEngine.GenerateCode(input, effectiveTemplateClassName, GeneratedTemplateNamespace, sourceFilename);
}
if (!res.Success)
throw new Exception("Failed to generate code");
var compileUnit = res.GeneratedCode;
var fullyQualifiedClassName = GeneratedTemplateNamespace + "." + effectiveTemplateClassName;
return new ParsedTemplate(fullyQualifiedClassName, compileUnit);
}
示例2: ParseToCode
public GeneratorResults ParseToCode(string TemplateCode, string defaultnamespace, string defaultclassname, string baseClassFullName)
{
GeneratorResults razorResults;
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
host.DefaultBaseClass = baseClassFullName;//typeof(BulaqTemplateForRazorBase).FullName;
host.DefaultNamespace = defaultnamespace;
host.DefaultClassName = defaultclassname;
host.NamespaceImports.Add("System");
host.NamespaceImports.Add("BulaqCMS.Models");
host.GeneratedClassContext = new GeneratedClassContext("Execute", "Write", "WriteLiteral");
var engine = new RazorTemplateEngine(host);
using (var reader = new StringReader(TemplateCode))
{
razorResults = engine.GenerateCode(reader);
CSharpCodeProvider codeProvider = new CSharpCodeProvider();
CodeGeneratorOptions options = new CodeGeneratorOptions();
options.BracingStyle = "C";
using (StringWriter writer = new StringWriter())
{
IndentedTextWriter indentwriter = new IndentedTextWriter(writer, " ");
codeProvider.GenerateCodeFromCompileUnit(razorResults.GeneratedCode, indentwriter, options);
indentwriter.Flush();
indentwriter.Close();
LastGeneratedCode = writer.GetStringBuilder().ToString();
string codePath = AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\\') + "\\code.cs";
File.WriteAllText(codePath, LastGeneratedCode, Encoding.UTF8);
}
}
return razorResults;
}
示例3: CompileCore
private async Task<CompilationResult> CompileCore(IFileInfo file)
{
var host = new MvcRazorHost();
var engine = new RazorTemplateEngine(host);
GeneratorResults results;
using (TextReader rdr = new StreamReader(file.CreateReadStream()))
{
results = engine.GenerateCode(rdr, '_' + Path.GetFileNameWithoutExtension(file.Name), "Asp", file.PhysicalPath ?? file.Name);
}
string generatedCode;
using (var writer = new StringWriter())
using (var codeProvider = new CSharpCodeProvider())
{
codeProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, writer, new CodeGeneratorOptions());
generatedCode = writer.ToString();
}
if (!results.Success)
{
return CompilationResult.Failed(generatedCode, results.ParserErrors.Select(e => new CompilationMessage(e.Message)));
}
Directory.CreateDirectory(_tempPath);
string tempFile = Path.Combine(_tempPath, Path.GetRandomFileName() + ".cs");
File.WriteAllText(tempFile, generatedCode);
_tempFileSystem.TryGetFileInfo(tempFile, out file);
return await _baseCompilationService.Compile(file);
}
示例4: GenerateCodeFromRazorString
private static string GenerateCodeFromRazorString(WebPageRazorHost host, string razorString, string virtualPath)
{
// Create Razor engine and use it to generate a CodeCompileUnit
var engine = new RazorTemplateEngine(host);
GeneratorResults results = null;
using (StringReader reader = new StringReader(razorString))
{
results = engine.GenerateCode(reader, className: null, rootNamespace: null,
sourceFileName: host.PhysicalPath);
}
if (!results.Success)
{
throw CreateExceptionFromParserError(results.ParserErrors.Last(), virtualPath);
}
// Use CodeDom to generate source code from the CodeCompileUnit
using (var codeDomProvider = new CSharpCodeProvider())
{
using (var srcFileWriter = new StringWriter())
{
codeDomProvider.GenerateCodeFromCompileUnit(results.GeneratedCode, srcFileWriter,
new CodeGeneratorOptions());
return srcFileWriter.ToString();
}
}
}
示例5: GetGeneratedCode
private CodeCompileUnit GetGeneratedCode()
{
try
{
if (this.generatedCode == null)
{
var engine = new RazorTemplateEngine(this.host);
GeneratorResults results;
using (var reader = this.OpenReader())
{
results = engine.GenerateCode(reader);
}
if (!results.Success)
{
throw new InvalidOperationException(results.ToString());
}
this.generatedCode = results.GeneratedCode;
}
return this.generatedCode;
}
catch (Exception ex)
{
Log.Error("GetGeneratedCode(): ", ex);
return null;
}
}
示例6: GetSyntaxTree
public override SyntaxTree GetSyntaxTree(string sourcePath, Stream sourceStream)
{
try
{
var viewFullPath = sourcePath;
var viewVirtualPath = GetRelativeUri(sourcePath, Compilation.CurrentDirectory.FullName);
var viewConfig = WebConfigurationManager.OpenMappedWebConfiguration(_configMap, viewVirtualPath);
var razorConfig = viewConfig.GetSectionGroup("system.web.webPages.razor") as RazorWebSectionGroup;
var host = razorConfig == null
? WebRazorHostFactory.CreateDefaultHost(viewVirtualPath, viewFullPath)
: WebRazorHostFactory.CreateHostFromConfig(razorConfig, viewVirtualPath, viewFullPath);
using (var rdr = new StreamReader(sourceStream, Compilation.Encoding, detectEncodingFromByteOrderMarks: true))
using (var provider = CodeDomProvider.CreateProvider("csharp"))
using (var generatedStream = new MemoryStream())
using (var generatedWriter = new StreamWriter(generatedStream, Compilation.Encoding))
{
var engine = new RazorTemplateEngine(host);
var razorOut = engine.GenerateCode(rdr, null, null, viewFullPath);
var codeGenOptions = new CodeGeneratorOptions { VerbatimOrder = true, ElseOnClosing = false, BlankLinesBetweenMembers = false };
provider.GenerateCodeFromCompileUnit(razorOut.GeneratedCode, generatedWriter, codeGenOptions);
// rewind
generatedWriter.Flush();
generatedStream.Position = 0;
return base.GetSyntaxTree(sourcePath, generatedStream);
}
}
catch (Exception ex)
{
Compilation.Diagnostics.Add(Diagnostic.Create(Compilation.ViewGenerationFailed, Compilation.AsLocation(sourcePath), ex.ToString()));
return null;
}
}
示例7: ReadHtmlSpans
void ReadHtmlSpans(string html)
{
RazorEngineHost razorEngineHost = new RazorEngineHost(codeLanguage);
RazorTemplateEngine engine = new RazorTemplateEngine(razorEngineHost);
ParserResults results = engine.ParseTemplate(new StringReader(html));
spans = new List<Span>(results.Document.Flatten());
spans.RemoveAll(span => span.Kind != SpanKind.Markup);
}
示例8: CreateRazorTemplateEngine
private static RazorTemplateEngine CreateRazorTemplateEngine()
{
var language = new CSharpRazorCodeLanguage();
var host = new SimpleRazorEngineHost(language);
var engine = new RazorTemplateEngine(host);
return engine;
}
示例9: ReadBlockSpans
void ReadBlockSpans(string markup)
{
var razorEngineHost = new RazorEngineHost(codeLanguage);
var engine = new RazorTemplateEngine(razorEngineHost);
var results = engine.ParseTemplate(new StringReader(markup));
spans = new List<Span>(results.Document.Flatten());
spans.RemoveAll(span => !span.IsBlock);
}
示例10: TemplateFactory
public TemplateFactory(RazorContext razorContext)
{
if (razorContext == null) throw new ArgumentNullException("razorContext");
_razorContext = razorContext;
_razorEngine = new RazorTemplateEngine(new XiptonEngineHost(razorContext.Config));
ContentManager = new ContentManager(razorContext.Config);
ContentManager.ContentProvider.ContentModified += OnContentModified;
ContentManager.SharedContentModified += OnSharedContentModified;
}
示例11: Parse
public IEnumerable<Span> Parse(string viewFile)
{
var codeLanguage = RazorCodeLanguageFactory.Create(viewFile.FileExtension());
using (var fileStream = new FileStream(viewFile, FileMode.Open, FileAccess.Read))
using (var reader = new StreamReader(fileStream))
{
var templateEngine = new RazorTemplateEngine(new RazorEngineHost(codeLanguage));
var parseResults = templateEngine.ParseTemplate(reader);
return parseResults.Document.Flatten();
}
}
示例12: RenderClientTemplate
public virtual void RenderClientTemplate(TextReader razorTemplate, TextWriter output)
{
var host = new RazorEngineHost(new CSharpRazorCodeLanguage());
var engine = new RazorTemplateEngine(host);
var parserResults = engine.ParseTemplate(razorTemplate);
if(parserResults.Success == false)
// TODO: Less suck
throw new RazorClientTemplateException("Template parse exception");
RenderClientTemplate(parserResults.Document, output);
}
示例13: Parse
internal void Parse(TextReader razorTemplate, TextWriter output)
{
RazorEngineHost host = new RazorEngineHost(new CSharpRazorCodeLanguage());
RazorTemplateEngine engine = new RazorTemplateEngine(host);
ParserResults result = engine.ParseTemplate(razorTemplate);
if (!result.Success)
{
ThrowParserError(result);
}
WriteTemplateFunction(result.Document, output);
}
示例14: GenerateCode
public GeneratorResults GenerateCode()
{
// Create the engine
RazorTemplateEngine engine = new RazorTemplateEngine(this);
// Generate code
using (Stream stream = File.OpenRead(_fullPath))
{
using (StreamReader reader = new StreamReader(stream, Encoding.Default, detectEncodingFromByteOrderMarks: true))
{
return engine.GenerateCode(reader);
}
}
}
示例15: CreateEngine
public static RazorTemplateEngine CreateEngine()
{
var language = new CSharpRazorCodeLanguage();
var host = new RazorEngineHost(language);
host.DefaultBaseClass = typeof(RazorTemplate<Context>).FullName;
host.DefaultNamespace = "RazorOutput";
host.DefaultClassName = "Template";
host.NamespaceImports.Add("System");
host.NamespaceImports.Add("System.IO");
//host.NamespaceImports.Add("System.Linq");
// host.NamespaceImports.Add("System.Text.RegularExpressions");
var engine = new RazorTemplateEngine(host);
return engine;
}