如何使用ASP.NET创建内容管理系统



我正在尝试创建一个供练习的乐器零售网站。为了总结我的项目,基本上,客户可以浏览仪器的选择,而管理员可以额外访问我在网站上要做的内容管理系统。然后,管理员可以添加或删除新的/旧产品,并且还可以通过GridView概述所有项目。

我的CMS的总体想法是,例如,我拥有称为" Ibanez"one_answers" Fender"的吉他品牌(自数据库分开以来,每个人都有自己的Griview)。我创建了一个网页,管理员将需要在文本框中填写所有必要的详细信息,以便在" Ibanez"one_answers" Fender"中创建一个项目,或者完全创建一个带有新项目的新品牌。如果管理员已经完成了信息,只需单击"保存",最后将新品牌或新产品出现在网站中。因此,假设管理员是否想创建一个名为" Gibson"的新品牌,该程序应在我的网站的概述部分中自动为该品牌创建一个gridview。

现在,为了实现这一切,我所做的是,当管理员命中保存以创建新品牌或产品时,我创建了一个代码,它将在某些项目文件中修改/编写新代码。它甚至可以通过添加.cs或.aspx扩展名来创建新文件,然后它将仅使用StreamWriter在新文件中进行WIRTE。

这是我的代码的示例,您在文件中编写新代码:

 var gridViewLines = File.ReadAllLines(@"C:UsersUser1DocumentsVisual Studio 2015WebSitesMusicStorePagesOverviewGuitarData.aspx");
    var _gridViewLines = new List<string>(gridViewLines);
    int index = counter;
    index -= 1;
    _gridViewLines.Insert(index++, "");
    _gridViewLines.Insert(index++, "    <h3>"+brand_name+" Guitar Items Data</h3>");
    _gridViewLines.Insert(index++, "    <div style="overflow:auto; width:1100px; max-height:500px;">");
    _gridViewLines.Insert(index++, "      <asp:GridView ID="Guitar" + brand_name + "GridView" runat="server" CssClass="mydatagrid" PagerStyle-CssClass="pager" HeaderStyle-CssClass="header" RowStyle-CssClass="rows" AllowPaging="True" AllowSorting="True" AutoGenerateColumns="False" CellPadding="3" CellSpacing="3" DataKeyNames="id" DataSourceID="SqlDataSource"+brand_number+"" OnRowDataBound="Guitar"+brand_name+"GridView_RowDataBound" Height="250px" Width="864px">");
    _gridViewLines.Insert(index++, "          <Columns>");
    _gridViewLines.Insert(index++, "              <asp:TemplateField>");
    _gridViewLines.Insert(index++, "                <ItemTemplate>");
    _gridViewLines.Insert(index++, "                     <asp:Button ID="Guitar"+brand_name+"GridViewBtn" runat="server" Text="Delete" OnClick="Guitar"+brand_name+"GridViewBtn_Click"/>");
    _gridViewLines.Insert(index++, "                </ItemTemplate>");
    _gridViewLines.Insert(index++, "              </asp:TemplateField>");
    _gridViewLines.Insert(index++, "              <asp:CommandField ButtonType="Button" ShowEditButton="True" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="id" HeaderText="id" ReadOnly="True" SortExpression="id" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="type" HeaderText="type" SortExpression="type" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="model" HeaderText="model" SortExpression="model" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="price" HeaderText="price" SortExpression="price" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="image1" HeaderText="image1" SortExpression="image1" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="image2" HeaderText="image2" SortExpression="image2" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="description" HeaderText="description" SortExpression="description" ItemStyle-Wrap="false" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="neck_type" HeaderText="neck_type" SortExpression="neck_type" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="body" HeaderText="body" SortExpression="body" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="fretboard" HeaderText="fretboard" SortExpression="fretboard" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="fret" HeaderText="fret" SortExpression="fret" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="bridge" HeaderText="bridge" SortExpression="bridge" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="neck_pickup" HeaderText="neck_pickup" SortExpression="neck_pickup" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="bridge_pickup" HeaderText="bridge_pickup" SortExpression="bridge_pickup" />");
    _gridViewLines.Insert(index++, "                 <asp:BoundField DataField="hardware_color" HeaderText="hardware_color" SortExpression="hardware_color" />");
    _gridViewLines.Insert(index++, "             </Columns>");
    _gridViewLines.Insert(index++, "         </asp:GridView>");
    _gridViewLines.Insert(index++, "      </div>");

    gridViewLines = _gridViewLines.ToArray();
    File.WriteAllLines(@"C:UsersUser1DocumentsVisual Studio 2015WebSitesMusicStorePagesOverviewGuitarData.aspx", gridViewLines);

我在这部分中取得了成功,但是当我尝试删除品牌时,我的代码不起作用,并给了我一个例外,它仍被另一个过程使用。

这是用于删除文件中一些文本的代码:

public static void SearchRemove()
    {
        int counter = 0;
        string line;
        // Read the file and display it line by line.
        System.IO.StreamReader file = new System.IO.StreamReader(@"C:UsersUser1DocumentsVisual Studio 2015WebSitesMusicStorePagesOverviewGuitarData.aspx");
        while ((line = file.ReadLine()) != null)
        {
            if (line.Contains("    <h3>Gibson Guitar Items Data</h3>"))
            {
                break;
            }
            counter+=1;
        }
        file.Close();
        Remove(counter);
  }

    public static void Remove(int counter)
    {

                int removeAt = counter;//or any thing you want
                removeAt -= 1;
                int linesToRemove = 70; //or any thing you want
                string s = System.IO.File.ReadAllText(@"C:UsersUser1DocumentsVisual Studio 2015WebSitesMusicStorePagesOverviewGuitarData.aspx");
                List<string> arr = s.Split("n".ToCharArray()).ToList();
                string result = "";
                for (int i = 0; i < linesToRemove; i++)
                {
                    arr.RemoveAt(removeAt);
                    result = "";
                    foreach (string str in arr)
                    {
                        result += str + "n";
                    }
                    System.IO.File.WriteAllText(@"C:UsersUser1DocumentsVisual Studio 2015WebSitesMusicStorePagesOverviewGuitarData.aspx", result);
                }
            }

    }              

当我不包括删除部分时,一切正常。后来,我还被告知我正在采用的方法是风险的,而不是网站工作的正确方法,因为管理员正在修改.cs或.aspx文件的内容。老实说,我一直在想一种更好的方法,所以我的问题是:

1。)如何在不修改/编写项目文件的情况下实现上述想法?

2。)如果我要继续以前的方法。如果我想删除我在文件中自动实现的新代码,而没有得到该过程仍在使用?

,则使用什么算法?

cmses通常将内容存储在数据库或文件系统中。代码(即.cs.aspx文件)不应该存储 content ,它仅包含 program logic

建议:

  1. 将您的内容存储在有效的XML文件中。示例:

    <?xml version="1.0" encoding="UTF-8"?>
    <brands>
        <brand name="Gibson">
            <items>
                ...
            </items>
        </brand>
    </brands>
    
  2. 从您的ASP.NET程序中读取XML文件,并将其验证为对象。另请参阅此MSDN主题:https://msdn.microsoft.com/en-us/library/58a18dwa(v = vs.110).aspx。

  3. 填写您的GridView或通常用您从XML文件中检索到的内容。
  4. 在XML文件上设置观察器,以便其内容更改时,您可以更新对象并在网站上呈现新数据。在MSDN上查看有关FileSystemWatcher的更多信息:https://msdn.microsoft.com/en-us/library/system.io.filesystemwatcher(v = vs.110).aspx

实现了这些内容,您的网站将不依赖于内容的代码,而是每次使用内容更新XML文件时会更新。然后,您可以探索如何将此信息存储在数据库中,以便您可以实现一些后端管理页面来管理内容(就像大多数CMSE一样)。

相关内容

最新更新