您将如何使用ASP.NET MVC在CMS中创建页面?

时间:2022-08-28 11:21:26

In a content management system you can usually create pages on the fly eg

在内容管理系统中,您通常可以动态创建页面,例如

www.website.com.au/home.aspx

www.website.com.au/projects.aspx

www.website.com.au/contact-us.aspx

In a previous CMS that we wrote we physically created these files on disk when the user selected to create a new page in his site. We copied the new file from a base template page, renamed the file and renamed the class in the code behind eg

在我们编写的先前CMS中,当用户选择在其站点中创建新页面时,我们在磁盘上物理创建了这些文件。我们从基本模板页面复制了新文件,重命名了该文件并在后面的代码中重命名了该类

template_page.aspx and template_page.aspx.cs turned into

template_page.aspx和template_page.aspx.cs变成了

projects.aspx and projects.aspx.cs

projects.aspx和projects.aspx.cs

This was all done via our CMS application. No files needed to be manually created by a user.

这都是通过我们的CMS应用程序完成的。用户无需手动创建任何文件。

How would this approach work using MVC?

这种方法如何使用MVC?

Eg www.website.com.au/home/

www.website.com.au/projects/

www.website.com.au/contact-us/

Presumably we would need to dynamically create controllers and views on the fly?

据推测,我们需要动态创建控制器和视图?

This seems even messier than the old approach but I suppose its feasible.

这似乎比旧方法更麻烦,但我认为它是可行的。

Can anyone think of a smarter way to do it?

谁能想到一个更聪明的方法呢?

1 个解决方案

#1


You should be able to use one controller and a couple views (display, create, edit) with some routing work. I did a super simple implementation for a personal project that went like this. I put this route near the top of my routing list and used the constraint to determine if it should be considered as a static page from my rules. My implementation didn't have any sort of hierarchy, i.e. pages/About-us/contact - only /contact.

您应该能够使用一个控制器和一些视图(显示,创建,编辑)和一些路由工作。我为这样的个人项目做了一个超级简单的实现。我将此路由放在路由列表的顶部附近,并使用约束来确定是否应将其视为我的规则中的静态页面。我的实现没有任何层次结构,即pages / About-us / contact - only / contact。

route:
routes.MapRoute("StaticContent", "{title}",
  new { controller = "Page", action = "Details"},
  new { title = new InvalidTitleContstraint()});


controller:
public class PageController : Controller
{
    // Details checks if it can find a matching title in the DB
    // redirects to Create if no match 
    public ActionResult Details(string title)
    // GET
    public ActionResult Create()
    // POST
    public ActionResult Create(Page page)
}

#1


You should be able to use one controller and a couple views (display, create, edit) with some routing work. I did a super simple implementation for a personal project that went like this. I put this route near the top of my routing list and used the constraint to determine if it should be considered as a static page from my rules. My implementation didn't have any sort of hierarchy, i.e. pages/About-us/contact - only /contact.

您应该能够使用一个控制器和一些视图(显示,创建,编辑)和一些路由工作。我为这样的个人项目做了一个超级简单的实现。我将此路由放在路由列表的顶部附近,并使用约束来确定是否应将其视为我的规则中的静态页面。我的实现没有任何层次结构,即pages / About-us / contact - only / contact。

route:
routes.MapRoute("StaticContent", "{title}",
  new { controller = "Page", action = "Details"},
  new { title = new InvalidTitleContstraint()});


controller:
public class PageController : Controller
{
    // Details checks if it can find a matching title in the DB
    // redirects to Create if no match 
    public ActionResult Details(string title)
    // GET
    public ActionResult Create()
    // POST
    public ActionResult Create(Page page)
}