当前位置: 首页>>代码示例>>C#>>正文


C# Razor.RazorTemplateEngine类代码示例

本文整理汇总了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);
        }
开发者ID:pragmatrix,项目名称:SiteSharper,代码行数:26,代码来源:TemplateParser.cs

示例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;
 }
开发者ID:abduwaris,项目名称:BulaqCMS,代码行数:30,代码来源:RazorTemplateParser.cs

示例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);
        }
开发者ID:464884492,项目名称:Mvc,代码行数:32,代码来源:RazorCompilationService.cs

示例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();
                }
            }
        }
开发者ID:jpolvora,项目名称:MvcLib.MyGet,代码行数:30,代码来源:RazorCompiler.cs

示例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;
            }
		}
开发者ID:grammarware,项目名称:fodder,代码行数:30,代码来源:src_ServiceStack_Razor_CSharpRazorBuildProvider.cs

示例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;
            }
        }
开发者ID:modulexcite,项目名称:StackExchange.Precompilation,代码行数:35,代码来源:RazorParser.cs

示例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);
		}
开发者ID:nylen,项目名称:SharpDevelop,代码行数:8,代码来源:RazorHtmlSpans.cs

示例8: CreateRazorTemplateEngine

        private static RazorTemplateEngine CreateRazorTemplateEngine()
        {
            var language = new CSharpRazorCodeLanguage();
            var host = new SimpleRazorEngineHost(language);
            var engine = new RazorTemplateEngine(host);

            return engine;
        }
开发者ID:nordbergm,项目名称:Simple.Web,代码行数:8,代码来源:RazorTypeBuilder.cs

示例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);
 }
开发者ID:RazorPad,项目名称:RazorPad,代码行数:8,代码来源:RazorSpans.cs

示例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;
 }
开发者ID:jrnail23,项目名称:RazorMachine,代码行数:9,代码来源:TemplateFactory.cs

示例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();
            }
        }
开发者ID:src75,项目名称:ToDoSample,代码行数:12,代码来源:ViewParser.cs

示例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);
        }
开发者ID:jchadwick,项目名称:RazorClientTemplates,代码行数:13,代码来源:RazorClientTemplateEngine.cs

示例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);
        }
开发者ID:bevacqua,项目名称:Razor.Js,代码行数:13,代码来源:RazorJsEngine.cs

示例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);
                }
            }
        }
开发者ID:ryanhaugh,项目名称:razorblade,代码行数:14,代码来源:RazorHost.cs

示例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;
        }
开发者ID:Xperterra,项目名称:Fhir.Publication,代码行数:15,代码来源:Razor.cs


注:本文中的System.Web.Razor.RazorTemplateEngine类示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。