代码之家  ›  专栏  ›  技术社区  ›  mohsinali1317

Episerver以编程方式创建页面

  •  0
  • mohsinali1317  · 技术社区  · 7 年前

    我正在使用此代码

            var parent = ContentReference.StartPage;
            IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>();
    
            PageData myPage = contentRepository.GetDefault<LoginPage>(parent);
            myPage.PageName = "My new page";
    
            var page = contentRepository.GetChildren<LoginPage>(parent).FirstOrDefault(name => name.Name == myPage.Name);
    
            if (page == null)
                contentRepository.Save(myPage, EPiServer.DataAccess.SaveAction.Publish);
    

    以编程方式创建页面。问题是我不确定该把代码放在哪里?

    我不想在管理/编辑面板的列表中显示LoginPage,因为我只想在该页面类型下创建一个页面。也许还有另一种方法,我可以只创建一个独立的页面,而不必创建页面类型,或者使用一个已经制作好的页面类型。

    这是我的页面类型的代码

    [ContentType(DisplayName = "Custom Login Page", GUID = "c0d358c3-4789-4e53-bef3-6ce20efecaeb", Description = "")]
    public class LoginPage : StandardPage
    {
        /*
                [CultureSpecific]
                [Display(
                    Name = "Main body",
                    Description = "The main body will be shown in the main content area of the page, using the XHTML-editor you can insert for example text, images and tables.",
                    GroupName = SystemTabNames.Content,
                    Order = 1)]
                public virtual XhtmlString MainBody { get; set; }
         */
    }
    

    public class LoginModel : PageViewModel<LoginPage>
    {
        public LoginFormPostbackData LoginPostbackData { get; set; } = new LoginFormPostbackData();
        public LoginModel(LoginPage currentPage)
            : base(currentPage)
        {
        }
        public string Message { get; set; }
    }
    
    public class LoginFormPostbackData
    {
        public string Username { get; set; }
        public string Password { get; set; }
        public bool RememberMe { get; set; }
        public string ReturnUrl { get; set; }
    }
    

        public ActionResult Index(LoginPage currentPage, [FromUri]string ReturnUrl)
        {
    
            var model = new LoginModel(currentPage);
            model.LoginPostbackData.ReturnUrl = ReturnUrl;
            return View(model);
        }
    

    你认为还有别的方法吗?我还将显示我的登录视图

    @using EPiServer.Globalization
    @model LoginModel
    
    <h1 @Html.EditAttributes(x => 
    x.CurrentPage.PageName)>@Model.CurrentPage.PageName</h1>
    <p class="introduction" @Html.EditAttributes(x => 
    x.CurrentPage.MetaDescription)>@Model.CurrentPage.MetaDescription</p>
    <div class="row">
    <div class="span8 clearfix" @Html.EditAttributes(x => 
    x.CurrentPage.MainBody)>
        @Html.DisplayFor(m => m.CurrentPage.MainBody)
    
    </div>
    

    @if (!User.Identity.IsAuthenticated && 
    !User.IsInRole("rystadEnergyCustomer"))
    {
    <div class="row">
        @using (Html.BeginForm("Post", null, new { language = ContentLanguage.PreferredCulture.Name }))
        {
            <div class="logo"></div>
            @Html.AntiForgeryToken()
    
            <h2 class="form-signin-heading">Log in</h2>
            @Html.LabelFor(m => m.LoginPostbackData.Username, new { @class = "sr-only" })
            @Html.TextBoxFor(m => m.LoginPostbackData.Username, new { @class = "form-control", autofocus = "autofocus" })
    
            @Html.LabelFor(m => m.LoginPostbackData.Password, new { @class = "sr-only" })
            @Html.PasswordFor(m => m.LoginPostbackData.Password, new { @class = "form-control" })
            <div class="checkbox">
                <label>
                    @Html.CheckBoxFor(m => m.LoginPostbackData.RememberMe)
                    @Html.DisplayNameFor(m => m.LoginPostbackData.RememberMe)
                </label>
            </div>
    
            @Html.HiddenFor(m => m.LoginPostbackData.ReturnUrl, "/login-customers")
            <input type="submit" value="Log in" class="btn btn-lg btn-primary btn-block" />
        }
        @Html.DisplayFor(m => m.Message)
    </div>
    }
    else
    {
    <span>Welcome @User.Identity.Name</span>
    @Html.ActionLink("Logout", "Logout", "LoginPage", null, null);
    }
    
    1 回复  |  直到 7 年前
        1
  •  2
  •   Ted Nyberg    7 年前

    我认为你误解了一些Episerver的概念。

    如果您不希望它成为Episerver中的页面,则不应使用PageController、页面类型或模板。相反,只需使用标准控制器和视图来创建登录页面。

    否则,您必须创建类型为的页面 LoginPage ,这将在页面树中可见。无需以编程方式创建它,您只需手动添加页面,然后隐藏 登录页面