private bool ValidateMarkup(string virtualPath, string content, out EntityToken newEntityToken) { newEntityToken = null; string filePath = PathUtil.Resolve(virtualPath); string fileName = Path.GetFileName(filePath); string tempFileName = RazorPageTemplateProvider.TempFilePrefix + fileName; string tempFileVirtualPath = virtualPath.Substring(0, virtualPath.Length - fileName.Length) + tempFileName; string tempFile = Path.Combine(Path.GetDirectoryName(filePath), tempFileName); try { C1File.WriteAllText(tempFile, content); WebPageBase webPageBase; try { webPageBase = WebPage.CreateInstanceFromVirtualPath(tempFileVirtualPath); } catch (Exception ex) { Log.LogWarning(LogTitle, "Compilation failed while validating changes to '{0}'", virtualPath); Log.LogWarning(LogTitle, ex); string message = ex.Message; if (message.StartsWith(tempFile, StringComparison.OrdinalIgnoreCase)) { message = fileName + message.Substring(tempFile.Length); } ShowWarning(GetText("EditTemplate.Validation.CompilationFailed") .FormatWith(message)); return(false); } if (!(webPageBase is RazorPageTemplate)) { if (IsPageTemplate) { var templateDescriptor = GetPageTemplateDescriptor(); if (templateDescriptor.IsValid) { ShowWarning(GetText("EditTemplate.Validation.IncorrectBaseClass") .FormatWith(typeof(RazorPageTemplate).FullName)); return(false); } newEntityToken = new SharedCodeFileEntityToken(virtualPath); return(true); } return(true); } Guid templateId; var pageTemplate = webPageBase as RazorPageTemplate; pageTemplate.Configure(); try { templateId = pageTemplate.TemplateId; } catch (Exception ex) { ShowPropertyError("TemplateId", ex); return(false); } try { string templateTitle = pageTemplate.TemplateTitle; } catch (Exception ex) { ShowPropertyError("TemplateTitle", ex); return(false); } if (!IsPageTemplate) { newEntityToken = new PageTemplateEntityToken(templateId); return(true); } var pageTemplateDescriptor = GetPageTemplateDescriptor(); if (pageTemplateDescriptor.IsValid) { // Forbidding to change template id from this workflow in order to avoid mistakes if (templateId != pageTemplateDescriptor.Id) { ShowWarning(GetText("EditTemplate.Validation.TemplateIdChanged").FormatWith(pageTemplateDescriptor.Id)); return(false); } } else { newEntityToken = new PageTemplateEntityToken(templateId); } } finally { C1File.Delete(tempFile); } return(true); }
private bool ValidateMarkup(string virtualPath, string content, out EntityToken newEntityToken) { newEntityToken = null; string filePath = PathUtil.Resolve(virtualPath); string fileName = Path.GetFileName(filePath); string tempFileName = RazorPageTemplateProvider.TempFilePrefix + fileName; string tempFileVirtualPath = virtualPath.Substring(0, virtualPath.Length - fileName.Length) + tempFileName; string tempFile = Path.Combine(Path.GetDirectoryName(filePath), tempFileName); try { C1File.WriteAllText(tempFile, content); WebPageBase webPageBase; try { webPageBase = WebPage.CreateInstanceFromVirtualPath(tempFileVirtualPath); } catch (Exception ex) { Log.LogWarning(LogTitle, "Compilation failed while validating changes to '{0}'", virtualPath); Log.LogWarning(LogTitle, ex); string message = ex.Message; if(message.StartsWith(tempFile, StringComparison.OrdinalIgnoreCase)) { message = fileName + message.Substring(tempFile.Length); } ShowWarning(GetText("EditTemplate.Validation.CompilationFailed") .FormatWith(message)); return false; } if (!(webPageBase is RazorPageTemplate)) { if(IsPageTemplate) { var templateDescriptor = GetPageTemplateDescriptor(); if(templateDescriptor.IsValid) { ShowWarning(GetText("EditTemplate.Validation.IncorrectBaseClass") .FormatWith(typeof(RazorPageTemplate).FullName)); return false; } newEntityToken = new SharedCodeFileEntityToken(virtualPath); return true; } return true; } Guid templateId; var pageTemplate = webPageBase as RazorPageTemplate; pageTemplate.Configure(); try { templateId = pageTemplate.TemplateId; } catch (Exception ex) { ShowPropertyError("TemplateId", ex); return false; } try { string templateTitle = pageTemplate.TemplateTitle; } catch (Exception ex) { ShowPropertyError("TemplateTitle", ex); return false; } if(!IsPageTemplate) { newEntityToken = new PageTemplateEntityToken(templateId); return true; } var pageTemplateDescriptor = GetPageTemplateDescriptor(); if (pageTemplateDescriptor.IsValid) { // Forbidding to change template id from this workflow in order to avoid mistakes if (templateId != pageTemplateDescriptor.Id) { ShowWarning(GetText("EditTemplate.Validation.TemplateIdChanged").FormatWith(pageTemplateDescriptor.Id)); return false; } } else { newEntityToken = new PageTemplateEntityToken(templateId); } } finally { C1File.Delete(tempFile); } return true; }