ToSic.SexyContent.Internal.TemplateManager.CreateTemplateFileIfNotExists C# (CSharp) Method

CreateTemplateFileIfNotExists() public method

Creates a template file if it does not already exists, and uses a default text to insert. Returns the new path
public CreateTemplateFileIfNotExists ( string name, string type, string location, HttpServerUtility server, string contents = "" ) : string
name string
type string
location string
server System.Web.HttpServerUtility
contents string
return string
        public string CreateTemplateFileIfNotExists(string name, string type, string location, HttpServerUtility server, string contents = "")
        {
            if (type == RazorC)
            {
                if (!name.StartsWith("_"))
                    name = "_" + name;
                if (Path.GetExtension(name) != ".cshtml")
                    name += ".cshtml";
            }
            else if (type == RazorVb)
            {
                if (!name.StartsWith("_"))
                    name = "_" + name;
                if (Path.GetExtension(name) != ".vbhtml")
                    name += ".vbhtml";
            }
            else if (type == TokenReplace)
            {
                if (Path.GetExtension(name) != ".html")
                    name += ".html";
            }

            var templatePath = Regex.Replace(name, @"[?:\/*""<>|]", "");
            var absolutePath = server.MapPath(Path.Combine(GetTemplatePathRoot(location, App), templatePath));

            if (!File.Exists(absolutePath))
            {
                var stream = new StreamWriter(File.Create(absolutePath));
                stream.Write(contents);
                stream.Flush();
                stream.Close();
            }

            return templatePath;
        }

Usage Example

示例#1
0
        /// <summary>
        /// After the Update button is clicked, updates the template or creates a new one,
        /// depending if in edit mode or not.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            var tm = new Internal.TemplateManager(App);

            var templatePath = ddlTemplateFiles.SelectedValue;

            if (!pnlSelectTemplateFile.Visible)
            {
                templatePath = tm.CreateTemplateFileIfNotExists(txtTemplateFileName.Text, ddlTemplateTypes.SelectedValue, ddlTemplateLocations.SelectedValue, Server, LocalizeString("NewTemplateFile.DefaultText"));
            }

            var templateId       = ModeIsEdit ? Template.TemplateId : new int?();
            var pipelineEntityId = ddlDataPipeline.SelectedValue == "0" ? (int?)null : Int32.Parse(ddlDataPipeline.SelectedValue);

            if (!chkSeparateContentPresentation.Checked)
            {
                ctrPresentationType.ContentTypeStaticName = "";
            }

            App.TemplateManager.UpdateTemplate(templateId, txtTemplateName.Text, templatePath, ctrContentType.ContentTypeStaticName, ctrContentType.DemoEntityID, ctrPresentationType.ContentTypeStaticName, ctrPresentationType.DemoEntityID, ctrListContentType.ContentTypeStaticName, ctrListContentType.DemoEntityID, ctrListPresentationType.ContentTypeStaticName, ctrListPresentationType.DemoEntityID, ddlTemplateTypes.SelectedValue, chkHidden.Checked, ddlTemplateLocations.SelectedValue, chkEnableList.Checked, chkPublishSource.Checked, txtPublishStreams.Text, pipelineEntityId, txtViewNameInUrl.Text);

            // old Redirect to the manage templates control
            //var RedirectUrl = UrlUtils.PopUpUrl(Globals.NavigateURL(SexyContent.ControlKeys.ManageTemplates, "mid", ModuleId.ToString(), SexyContent.AppIDString, AppId.ToString()), this, PortalSettings, false, true);
            //Response.Redirect(RedirectUrl);

            // New 2015-09-19 close this window - temporary solution till this dialog is obsolete
            Response.Write("<script>window.close();</script>");
        }