简体   繁体   English

如何使用Asp.Net MVC C#创建CMS应用程序

[英]How to Create a CMS Application Using Asp.Net MVC C#

I had reviewed lots of questions already posted on StackOverFlow and most of the answers ended up on what CMS platform to use instead of how to actually create one from Scratch. 我已经回顾了StackOverFlow上已经发布的很多问题,大部分答案都归结为使用什么CMS平台而不是如何从Scratch实际创建一个。

Searching the Internet and Books also did not result in any useful answers to my questions. 搜索互联网和书籍也没有对我的问题产生任何有用的答案。

I am fairly new to using Asp.Net MVC and wanted to know how to make a very basic CMS with Asp.Net, MVC, Entity Framework, in C#. 我很擅长使用Asp.Net MVC,并想知道如何使用Asp.Net,MVC,Entity Framework在C#中创建一个非常基本的CMS。

My main issue is creating database driven pages that are not hard coded into the MVC Controller and not physically located as Somepage.cshtml 我的主要问题是创建数据库驱动的页面,这些页面没有硬编码到MVC控制器中,也没有物理上位于Somepage.cshtml

Would like to know how to do the following: 想知道如何做到以下几点:

Incoming URL: MainWebsite.com/DatabaseProvidedPage 传入URL:MainWebsite.com/DatabaseProvidedPage

Goal is for the controller in Route for pages without a /controller/ in the URL to default as being a Database Driven/Created Webpage. 目标是路由中的控制器,没有URL中的/ controller /的页面默认为数据库驱动/创建的网页。

Would like to actually know how to do this. 想知道如何做到这一点。

Not looking for a platform to use to do this. 不寻找用于执行此操作的平台。

I want to learn the basics of creating a CMS using Asp.Net MVC. 我想学习使用Asp.Net MVC创建CMS的基础知识。

Just really want: 真的很想要:

How to do this? 这个怎么做?

URL: MainWebsite/DatabaseProvidedWebpage --> Return to user the HTML stored in database URL:MainWebsite / DatabaseProvidedWebpage - >返回用户存储在数据库中的HTML

Thanks, 谢谢,

Found Solution: 找到解决方案

Routing 路由

// Display Database Provided Page MainSite.Com/{PageName}

routes.MapRoute("DatabasePage", 
"{PageName}", 
new { controller = "Home", 
action = "Index", 
PageName = "" 
});

Controller: 控制器:

public ActionResult Index(String PageName)
        {
            if (PageName == null)
                {
                    List<PageTable> pages = PagesRepository.GetAllPages();
                    return View(pages);
                }

            PageTable FindPageName = PagesRepository.GetPageByURL(PageName);



            if (FindPageName == null)
                {
                    return Content(PageName);
                }


            if (FindPageName != null)
                {
                    return View(FindPageName);
                }

            return View("");

        }

View 视图

@model CMS_Repository.DAL.EF.PageTable

@{
ViewBag.Title = "Details";
}

<h2>Details</h2>

    <div>
    <h4>PageTable</h4>
    <hr />
    <dl class="dl-horizontal">
        <dt>
            @Html.DisplayNameFor(model => model.Title)
            </dt>

        <dd>
            @Html.DisplayFor(model => model.Title)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.Content)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.Content)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.FriendlyURL)
        </dt>

        <dd>
            @Html.DisplayFor(model => model.FriendlyURL)
        </dd>

        <dt>
            @Html.DisplayNameFor(model => model.ControllerName)
        </dt>


</dl>

Scaffolding does this automatically for you, only not with Guids. 脚手架会自动为您执行此操作,但不会使用Guids。 When you scaffold, you get a controller with several actions, one of which is Details. 当你使用脚手架时,你会得到一个带有多个动作的控制器,其中一个是细节。 By accessing /Controller/Details/[id] you see details about the specified item. 通过访问/ Controller / Details / [id],您可以看到有关指定项目的详细信息。 Create a table with a column for your HTML; 创建一个包含HTML列的表格; let's call it HTML. 我们称之为HTML。 In the Details view, write 在Details视图中,编写

@Html.Raw(Model.HTML)

I recommend Microsoft's Introduction to MVC 5 . 我推荐微软的MVC 5简介 You'll create a website based on their starter project, and it should answer most of your questions on how you can create a CMS. 您将根据他们的入门项目创建一个网站,它应该回答您关于如何创建CMS的大部分问题。

In MVC, use the concept of display/editor templates. 在MVC中,使用显示/编辑器模板的概念。 They allow you to: 他们允许您:

  • Have more abstract views. 有更多抽象的观点。 Views then serve rather as more-specific layouts (in conjunction with your more-generic _Layout.cshtml). 然后,视图作为更具体的布局(与更通用的_Layout.cshtml结合使用)。
  • Store specific pages as objects of various CLR types in your persistent repository (be it EF or anything else) 将特定页面存储为持久存储库中的各种CLR类型的对象(无论是EF还是其他任何东西)

The objects should be strongly typed and are supposed to be hierarchically composed of/aggregated of other objects representing the internal parts of the page. 对象应该是强类型的,并且应该由表示页面内部部分的其他对象的层次组成/聚合。

Your views may then look like: 您的观点可能如下所示:

@model BikeStore.Models.BodyPartTwoSecondaryParts

<div class="container body-part">
    @Html.DisplayFor(vm => vm.BodyPart)
</div>
<div class="container">
    <div class="inline-block half-width secondary-part">
        @Html.DisplayFor(vm => vm.FirstSecondaryPart)
    </div>
    <div class="inline-block half-width secondary-part">
        @Html.DisplayFor(vm => vm.SecondSecondaryPart)
    </div>
</div>

It is possible because MVC can automatically render a display/editor template once it sees that data of a certain CLR type appears in the view model. 这是可能的,因为一旦MVC看到某个CLR类型的数据出现在视图模型中,它就可以自动呈现显示/编辑器模板。 The template is basically an ordinary Razor file that has the same name as the CLR type. 该模板基本上是一个普通的Razor文件,其名称与CLR类型相同。

The pages may even have several visual representations (templates) defined. 这些页面甚至可以定义几个可视化表示(模板)。 It is possible to invoke them in various parts of the app (ie in various views) via custom HTML helpers: 可以通过自定义HTML帮助程序在应用程序的各个部分(即在各种视图中)调用它们:

For instance, a SearchResults.cshtml view might just call: 例如,SearchResults.cshtml视图可能只调用:

@foreach (var item in Model.SearchResults)
{
    @Html.SearchResultFor(vm => item)
}

The HTML helper method would be: HTML帮助器方法将是:

public static MvcHtmlString SearchResultFor<TModel, TValue>(this HtmlHelper<TModel> htmlHelper, Expression<Func<TModel, TValue>> expression)
{
    string typeName = expression.ReturnType.Name;

    return htmlHelper.DisplayFor(expression, $"{typeName}Search");
}

Another question is how to store pages of arbitrarily deep structures via Entity Framework. 另一个问题是如何通过Entity Framework存储任意深度结构的页面。 (I ommit the option to store their full HTML as nvarchar data as this is what a CMS should help you to avoid.) (我省略了将完整HTML存储为nvarchar数据的选项,因为这是CMS应该帮助您避免的。)

I cannot tell how complex such code would be. 我不知道这样的代码会有多复杂。 You might probably get inspired by a JSON de-serializing logic in our company's SDK. 您可能会受到我们公司SDK中的JSON反序列化逻辑的启发。 That method builds a strongly typed model that you can then pass into MVC views and rely on display templates. 该方法构建一个强类型模型,然后您可以将其传递到MVC视图并依赖于显示模板。

If you wish to see the above code examples in context, take a look at one of my posts . 如果您希望在上下文中看到上面的代码示例,请查看我的一篇帖子 You can also examine a working example app that's using our headless CMS as its persistent repository. 您还可以检查使用我们的无头CMS作为其持久性存储库的工作示例应用程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM