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


C# TemplateService.ParseAndReturnSource方法代码示例

本文整理汇总了C#中TemplateService.ParseAndReturnSource方法的典型用法代码示例。如果您正苦于以下问题:C# TemplateService.ParseAndReturnSource方法的具体用法?C# TemplateService.ParseAndReturnSource怎么用?C# TemplateService.ParseAndReturnSource使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在TemplateService的用法示例。


在下文中一共展示了TemplateService.ParseAndReturnSource方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。

示例1: Main

        static void Main(string[] args)
        {
            string templatebasename = "RazorEngine.Templating.TemplateBase";
            if (args.Length > 0)
            {
                templatebasename = args[0];
            }

            string classNamespace = "Brinkhill.Mobile.Publishing.Views";
            if (args.Length > 1)
            {
                classNamespace = args[1];
            }

            var filecount = 0;

            Console.WriteLine("Generating .cshtml.cs classes based on {0}", templatebasename);

            // for each .cshtml file under the working directory, generate a .cs file if it has changed.
            foreach (var templatepath in Directory.EnumerateFiles(Environment.CurrentDirectory, "*.cshtml", SearchOption.AllDirectories))
            {
                FileInfo fitemplate = new FileInfo(templatepath);
                FileInfo ficode = new FileInfo(templatepath + ".cs");

                try
                {
                    if (!ficode.Exists || ficode.LastWriteTimeUtc < fitemplate.LastWriteTimeUtc)
                    {
                        // get classname from path
                        var cn = fitemplate.Name.Substring(0, fitemplate.Name.IndexOf('.'));
                        var pt = fitemplate.DirectoryName.Split(new char[] { Path.DirectorySeparatorChar }, StringSplitOptions.RemoveEmptyEntries);
                        var ns = pt[pt.Length - 1];
                        for (var i = pt.Length - 2; i > 0; i--)
                        {
                            ns = pt[i] + "." + ns;
                            if (pt[i + 1] == "Views") break;
                        }

                        string template =
                             File.ReadAllText(fitemplate.FullName);
                        //Razor.SetTemplateBaseType(typeof(TemplateBase<>));

                        string viewModelName = string.Empty;

                        string baseTypeName = templatebasename;
                        if (template.StartsWith("@model"))
                        {
                            var l1 = template.IndexOf("\n");
                            viewModelName = template.Substring(6, l1 - 6).Trim();
                            template = template.Substring(l1).Trim();
                            baseTypeName = templatebasename + "<" + viewModelName + ">";
                        }
                        else if (cn == "_ViewStart")
                        {
                            baseTypeName = "System.Web.WebPages.StartPage";
                        }

                        CompilerServiceBuilder.SetCompilerServiceFactory(new DefaultCompilerServiceFactory());
                        RazorEngine.Configuration.DefaultTemplateServiceConfiguration conf = new RazorEngine.Configuration.DefaultTemplateServiceConfiguration();
                        conf.Debug = true;

                        conf.ReplacementClassName = cn;
                        conf.ReplacementViewModelName = viewModelName;
                        conf.ReplacementNamespace = classNamespace;

                        string result = string.Empty;

                        using (var service = new TemplateService(conf))
                        {
                            try
                            {
                                result = service.ParseAndReturnSource(template);
                            }
                            catch (TemplateCompilationException ex)
                            {
                                result = ex.SourceCode;
                            }
                        }

                        File.WriteAllText(ficode.FullName, result);
                        Console.WriteLine("Updated {0}.{1}", ns, cn);
                        filecount++;
                    }
                    else
                    {
                        Console.WriteLine("Skipping unchanged view {0}", fitemplate.Name);
                    }
                }
                catch (Exception ex)
                {
                    Console.WriteLine("Error updating {0} - {1}", ficode.Name, ex.Message);
                    Environment.Exit(1);
                }
            }
            Console.WriteLine("Done - updated {0} files", filecount);
        }
开发者ID:MikkoCodes,项目名称:RazorEngine,代码行数:96,代码来源:Program.cs


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