Episerver以编程方式创建页面

Episerver以编程方式创建页面,episerver,Episerver,我正在使用这个代码 var parent = ContentReference.StartPage; IContentRepository contentRepository = EPiServer.ServiceLocation.ServiceLocator.Current.GetInstance<IContentRepository>(); PageData myPage = contentRepository.GetDefaul

我正在使用这个代码

        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);
然后我创建了一个这样的模型

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>
@使用epserver.Globalization
@模型逻辑模型
x、 CurrentPage.PageName)>@Model.CurrentPage.PageName

x、 CurrentPage.MetaDescription)>@Model.CurrentPage.MetaDescription

x、 当前页面(主体)> @DisplayFor(m=>m.CurrentPage.MainBody)

@if(!User.Identity.IsAuthenticated&&
!User.IsInRole(“rystadEnergyCustomer”))
{
@使用(Html.BeginForm(“Post”,null,new{language=ContentLanguage.PreferredCulture.Name}))
{
@Html.AntiForgeryToken()
登录
@LabelFor(m=>m.LoginPostbackData.Username,新的{@class=“sr only”})
@TextBoxFor(m=>m.LoginPostbackData.Username,新的{@class=“form control”,autofocus=“autofocus”})
@LabelFor(m=>m.LoginPostbackData.Password,新的{@class=“sr only”})
@PasswordFor(m=>m.LoginPostbackData.Password,新的{@class=“form control”})
@CheckBoxFor(m=>m.LoginPostbackData.RememberMe)
@DisplayNameFor(m=>m.LoginPostbackData.RememberMe)
@Html.HiddenFor(m=>m.LoginPostbackData.ReturnUrl,“/login customers”)
}
@DisplayFor(m=>m.Message)
}
其他的
{
欢迎@User.Identity.Name
@ActionLink(“注销”、“注销”、“登录页面”、null、null);
}

我认为您误解了一些epserver概念

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


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

我认为您误解了一些Episerver概念

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


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

谢谢@Ted我尝试了以普通方式或标准方式创建页面,但无法像在MVC controller中那样访问它。也许我需要为它创建一个路由映射?是的,您需要添加一个默认路由,以便使用URL访问标准控制器,如
/[Controller]/[Action]
。谢谢@Ted,我尝试了创建普通方式或标准方式,但后来我无法像在MVC Controller中那样访问它。也许我需要为它创建一个路由映射?是的,您需要添加一个默认路由,以便使用URL访问标准控制器,如
/[Controller]/[Action]
@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);
}