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); }
public override void ModifyTags(SolutionItem policyParent, Project project, string language, string identifier, string fileName, ref Dictionary <string, string> tags) { base.ModifyTags(policyParent, project, language, identifier, fileName, ref tags); if (fileName == null) { return; } tags ["AspNetMaster"] = ""; tags ["AspNetMasterContent"] = ""; AspNetAppProject aspProj = project as AspNetAppProject; if (aspProj == null) { throw new InvalidOperationException("MasterContentFileDescriptionTemplate is only valid for ASP.NET projects"); } ProjectFile masterPage = null; var dialog = new MonoDevelop.Ide.Projects.ProjectFileSelectorDialog(aspProj, null, "*.master"); try { dialog.Title = GettextCatalog.GetString("Select a Master Page..."); int response = MonoDevelop.Ide.MessageService.RunCustomDialog(dialog); if (response == (int)Gtk.ResponseType.Ok) { masterPage = dialog.SelectedFile; } } finally { dialog.Destroy(); } if (masterPage == null) { return; } tags ["AspNetMaster"] = aspProj.LocalToVirtualPath(masterPage); try { var pd = TypeSystemService.ParseFile(project, masterPage.FilePath) as WebFormsParsedDocument; if (pd == null) { return; } var sb = new System.Text.StringBuilder(); foreach (string id in pd.XDocument.GetAllPlaceholderIds()) { sb.Append("<asp:Content ContentPlaceHolderID=\""); sb.Append(id); sb.Append("\" ID=\""); sb.Append(id); sb.Append("Content\" runat=\"server\">\n</asp:Content>\n"); } tags["AspNetMasterContent"] = sb.ToString(); } catch (Exception ex) { //no big loss if we just insert blank space //it's just a template for the user to start editing LoggingService.LogWarning("Error generating AspNetMasterContent for template", ex); } }