我需要一个简单的功能来在这些页面上添加页面/更改内容。我已经查看了 n2 和其他预构建 CMS 工具,但这些都是我需要的简单 CMS 功能的高级方法。
最好的方法是什么?我已经有一个 MVC 应用程序,我想添加/构建一个简单的功能,例如:
不知道从哪里开始。
任何信息都非常感谢。
谢谢
这是用于 .NET MVC
最佳答案
假设您使用的是 ASP.NET MVC,并且希望保持简单,那么这样的事情怎么样:
public abstract class TemplateBase
{
public abstract string TemplateName { get; }
}
public class SingleColumnTemplate : TemplateBase
{
public override string TemplateName { get { return "Single-column page"; } }
public AreaContainer CenterColumn { get; protected set; }
public SingleColumnTemplate()
{
CenterColumn = new AreaContainer("Center column");
}
}
public class TwoColumnTemplate : TemplateBase
{
public override string TemplateName { get { return "Two-column page"; } }
public AreaContainer LeftColumn { get; protected set; }
public AreaContainer RightColumn { get; protected set; }
public TwoColumnTemplate()
{
LeftColumn = new AreaContainer("Left column");
RightColumn = new AreaContainer("Right column");
}
}
// TODO Add more template types
public class AreaContainer
{
public string ContainerName { get; set; }
public IList<AreaBase> Areas { get; protected set; }
public AreaContainer(string name)
{
ContainerName = name;
Areas = new List<AreaBase>();
}
}
public abstract class AreaBase
{
public abstract string AreaName { get; }
}
public class HtmlArea : AreaBase
{
public override string AreaName { get { return "HTML content"; } }
public string HtmlContent { get; set; }
}
// TODO Add more area types
public class Page
{
public int Id { get; set; }
public string Title { get; set; }
public TemplateBase Template { get; set; }
}
用于编辑现有页面的 Controller 操作可能类似于:
public class PageAdminController : Controller
{
[HttpGet]
ActionResult Edit(int id)
{
var page = GetPageFromStorageById(id);
// TODO If the page is not found, issue 404
return View(page);
}
// ...
}
在 View (
Views/PageAdmin/Edit.aspx
) 中,应该强类型为 ViewPage<Page>
,您可以使用 HtmlHelper.EditorFor(...)
渲染适当模板 View 的方法,前提是您为每种模板类型创建了部分 View :<!-- Inside the edit view for Page (Edit.aspx) -->
<%: Html.HiddenFor(m => m.Id) %>
<%: Html.EditorFor(m => m.Title) %>
<%: Html.EditorFor(m => m.Template) %>
在文件夹中
Views/PageAdmin/EditorTemplates
然后,您将为每个模板和区域类型(即 SingleColumnTemplate.ascx
、 TwoColumnTemplate.ascx
和 HtmlArea.ascx
)放置部分编辑 View 。您可能还想为 AreaContainer
创建一个局部 View 。 .至于接收编辑页面的 Controller Action ,事情变得有点复杂。由于
Page
具有 TemplateBase
类型的属性,这是一个抽象类,DefaultModelBinder
不知道如何填充它。您可以通过编写自定义模型绑定(bind)器来解决此问题,该绑定(bind)器以某种方式“知道”要实例化哪个实现类。它怎么会知道呢?我能想到的一种选择是在 View 中包含一个隐藏字段,该字段包含页面模板的实际运行时类型的名称。我想这有点像黑客,但既然你追求简单,我认为没关系。在这种情况下,只需包含一个名为 RuntimeTypeName
的属性即可。在 TemplateBase
类(class):public string RuntimeTypeName { get { return GetType().FullName; } }
因为它只是调用
GetType()
,这是一个默认被所有类型覆盖的虚方法,它将返回运行时模板类型的名称。然后您必须确保您为
TemplateBase
创建的部分 View 实现包括 TemplateBase.RuntimeTypeName
的(隐藏)字段属性(property)。换句话说,在 SingleColumnTemplate.ascx
和 TwoColumnTemplate.ascx
你会有这条线:<%: Html.HiddenFor(m => m.RuntimeTypeName) %>
利用此信息创建正确类型的模板的模型绑定(bind)器可能如下所示:
/// <summary>
/// Model binder hack that builds upon the DefaultModelBinder,
/// but that can detect the "proper" subclass/implementing class
/// type for a model, assuming the name of that type is contained
/// in a field called "RuntimeTypeName".
/// </summary>
public class InheritanceSupportingModelBinder : DefaultModelBinder
{
// Assume that the name of the field that contains the
// runtime type name is called "RuntimeTypeName"
public const string RuntimeTypeNameField = "RuntimeTypeName";
private Type RuntimeType { get; set; }
// This method is called by the DefaultModelBinder to find out which
// properties of the current model that it should attempt to bind
protected override PropertyDescriptorCollection GetModelProperties(
ControllerContext controllerContext, ModelBindingContext bindingContext)
{
// If we have found out the runtime type of the model through
// looking at the "special" field above, use the properties of that type.
// Otherwise, use the default behavior.
if (RuntimeType != null)
{
return TypeDescriptor.GetProperties(RuntimeType);
}
else
{
return base.GetModelProperties(controllerContext, bindingContext);
}
}
// This method is called by the DefaultModelBinder when it
// tries to create an instance of the model class. If the
// class is abstract, an exception will be thrown. Therefore
// we try to read the name of the actual type from the
// RuntimeTypeName (hidden) field and return an instance of that type.
protected override object CreateModel(ControllerContext controllerContext,
ModelBindingContext bindingContext,
Type modelType)
{
if (bindingContext.ValueProvider.ContainsPrefix(
bindingContext.ModelName + "." + RuntimeTypeNameField))
{
var result = bindingContext.ValueProvider.GetValue(
bindingContext.ModelName + "." + RuntimeTypeNameField);
if (result != null && !string.IsNullOrEmpty(result.AttemptedValue))
{
// Check that the type indicated by the hidden field is really
// a subclass of (or implementing) the indicated base class
var tempType = Type.GetType(result.AttemptedValue);
if (modelType.IsAssignableFrom(tempType))
{
RuntimeType = modelType = tempType;
}
}
}
return base.CreateModel(controllerContext, bindingContext, modelType);
}
}
免责声明:我自己是 ASP.NET MVC 的初学者,所以这个模型绑定(bind)器很可能有问题。我通过查看
DefaultModelBinder
的源代码将它们放在一起。并通过反复试验。这只是一个例子,但根据我的(快速而肮脏的)测试,它似乎有效。当然,您需要在 Global.asax 中注册它才能生效:
ModelBinders.Binders.Add(
typeof(TemplateBase),
new InheritanceSupportingModelBinder());
但我们还没有完成!请记住
AreaContainer.Areas
集合的类型为 IList<AreaBase>
- 从 AreaBase
开始也是一个抽象类,我们必须应用相同的 hack 才能正确绑定(bind)它。也就是说,添加 RuntimeTypeName
AreaBase
的属性(property)类并为 AreaBase
注册我们的自定义模型绑定(bind)器类(class) Global.asax
.如果到目前为止我们已经遵循了所有这些步骤,我们可以在我们的
PageAdminController
上设置一个操作方法。用于处理看起来像这样的页面的编辑:[HttpPost]
public ActionResult Edit(Page page)
{
if (!ModelState.IsValid)
{
return View(page);
}
// TODO Save page to database or whatever
// TODO Redirect to page index
}
创建新页面的操作方法留作练习,应该不难(用户从列表中选择模板,显示正确的表单,后处理操作如上)。
显示页面应该很简单,只需使用
HtmlHelper.DisplayFor(...)
而不是 EditorFor(...)
,创建相应的局部 View 并设置好。对于所见即所得的内容编辑,您可能需要使用第三方组件。 CKEditor , TinyMCE , YUI Rich Text Editor和 Telerik Editor是一些例子。
这就是我的看法!欢迎所有评论;正如我提到的,我自己正在学习 ASP.NET MVC,如果我的错误被更了解的人指出,那就太好了。
关于asp.net-mvc - 构建 MVC CMS,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4097045/