상속: System.Web.WebPages.Razor.WebPageRazorHost
예제 #1
0
        public static string GenerateCodeWithAspNetRazorViewEngine(string razorTemplatePath)
        {
            //-- Configure the code-generator
            var host = new MvcWebPageRazorHost("/", razorTemplatePath);

            //-- Parse the template into a CodeDOM graph (see http://msdn.microsoft.com/en-us/library/y2k85ax6(v=vs.110).aspx for an overview of what CodeDOM is)
            var engine = new RazorTemplateEngine(host);
            GeneratorResults results;

            using (var reader = new StringReader(File.ReadAllText(razorTemplatePath)))
            {
                results = engine.GenerateCode(reader);
            }

            //-- Generate C# code from the CodeDOM graph
            var builder = new StringBuilder();
            using (var writer = new StringWriter(builder, CultureInfo.InvariantCulture))
            {
                new CSharpCodeProvider().GenerateCodeFromCompileUnit(results.GeneratedCode, writer, new CodeGeneratorOptions());
                return builder.ToString();
            }
        }
예제 #2
0
		RazorEngineHost CreateRazorHost (string fileName)
		{
			string virtualPath = "~/Views/Default.cshtml";
			if (project != null)
				virtualPath = project.LocalToVirtualPath (fileName);

			WebPageRazorHost host = null;

			// Try to create host using web.config file
			var webConfigMap = new WebConfigurationFileMap ();
			var vdm = new VirtualDirectoryMapping (project.BaseDirectory.Combine ("Views"), true, "web.config");
			webConfigMap.VirtualDirectories.Add ("/", vdm);
			Configuration configuration;
			try {
				configuration = WebConfigurationManager.OpenMappedWebConfiguration (webConfigMap, "/");
			} catch {
				configuration = null;
			}
			if (configuration != null) {
				var rws = configuration.GetSectionGroup (RazorWebSectionGroup.GroupName) as RazorWebSectionGroup;
				if (rws != null) {
					host = WebRazorHostFactory.CreateHostFromConfig (rws, virtualPath, fileName);
					host.DesignTimeMode = true;
				}
			}

			if (host == null) {
				host = new MvcWebPageRazorHost (virtualPath, fileName) { DesignTimeMode = true };
				// Add default namespaces from Razor section
				host.NamespaceImports.Add ("System.Web.Mvc");
				host.NamespaceImports.Add ("System.Web.Mvc.Ajax");
				host.NamespaceImports.Add ("System.Web.Mvc.Html");
				host.NamespaceImports.Add ("System.Web.Routing");
			}

			return host;
		}
예제 #3
0
		RazorEngineHost CreateRazorHost (string fileName)
		{
			if (project != null) {
				var projectFile = project.GetProjectFile (fileName);
				if (projectFile != null && projectFile.Generator == "RazorTemplatePreprocessor") {
					return new MonoDevelop.AspNet.Razor.Generator.PreprocessedRazorHost (fileName) {
						DesignTimeMode = true,
						EnableLinePragmas = false,
					};
				}
			}

			string virtualPath = "~/Views/Default.cshtml";
			if (aspProject != null)
				virtualPath = aspProject.LocalToVirtualPath (fileName);

			WebPageRazorHost host = null;

			// Try to create host using web.config file
			var webConfigMap = new WebConfigurationFileMap ();
			if (aspProject != null) {
				var vdm = new VirtualDirectoryMapping (aspProject.BaseDirectory.Combine ("Views"), true, "web.config");
			webConfigMap.VirtualDirectories.Add ("/", vdm);
			}
			Configuration configuration;
			try {
				configuration = WebConfigurationManager.OpenMappedWebConfiguration (webConfigMap, "/");
			} catch {
				configuration = null;
			}
			if (configuration != null) {
				var rws = configuration.GetSectionGroup (RazorWebSectionGroup.GroupName) as RazorWebSectionGroup;
				if (rws != null) {
					host = WebRazorHostFactory.CreateHostFromConfig (rws, virtualPath, fileName);
					host.DesignTimeMode = true;
				}
			}

			if (host == null) {
				host = new MvcWebPageRazorHost (virtualPath, fileName) { DesignTimeMode = true };
				// Add default namespaces from Razor section
				host.NamespaceImports.Add ("System.Web.Mvc");
				host.NamespaceImports.Add ("System.Web.Mvc.Ajax");
				host.NamespaceImports.Add ("System.Web.Mvc.Html");
				host.NamespaceImports.Add ("System.Web.Routing");
			}

			return host;
		}
        private CodeCompileUnit GetGeneratedCode(string virtualPath, TextReader reader)
        {
            var host = new MvcWebPageRazorHost(virtualPath, string.Empty)
            {
                DefaultBaseClass = typeof (WebViewPageSimulator).FullName
            };

            foreach(var ns in _namespaces)
                host.NamespaceImports.Add(ns);

            var generatedCode =
                new RazorTemplateEngine(host).GenerateCode(reader).GeneratedCode;
            return generatedCode;
        }