循环优化(多级菜单)ASP.NET

时间:2014-08-13 04:29:54

标签: asp.net-mvc entity-framework file

我正在尝试创建一个函数来为多语言菜单生成HTML文件。代码可以工作,但唯一的问题是编译时间太长。流程是,此函数将在Global.asax文件中调用(启动程序之前的过程)

我怀疑它是由循环过程引起的。所以请告诉我有关优化的建议。先感谢您。这是代码。

 public static void GenerateMenu(string strPath)
{
    string[] tempCateHTML = new string[] { "_temp_menu_en.cshtml", "_temp_menu_bm.cshtml", "_temp_menu_ch.cshtml" }; //temporary file names
    string[] cateHTML = new string[] { "_menu_en.cshtml", "_menu_bm.cshtml", "_menu_ch.cshtml" };
    for (int i = 0; i < 3; i++)
    {

        using (EFContext ctx = new EFContext())
        {
            List<int> parentId = new List<int>();
            List<Category> parentCategory = ctx.Database.SqlQuery<Category>
                ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = 0").ToList();

            StringWriter sWriter = new StringWriter();
            using (HtmlTextWriter wt = new HtmlTextWriter(sWriter))
            {
                 foreach (Category cate in parentCategory)
                {

                    string[] columnParent = new string[] { cate.Category_Name, cate.Category_NameBM, cate.Category_NameCH };
                    wt.RenderBeginTag(HtmlTextWriterTag.Li); //<li>
                    wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                    wt.RenderBeginTag(HtmlTextWriterTag.A); //<a>
                    wt.Write(columnParent[i]);
                    wt.RenderEndTag();//</a>

                    wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                    List<Category> childCategoryL1 = ctx.Database.SqlQuery<Category>
                        ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", cate.CategoryID).ToList();

                   foreach (Category root in childCategoryL1)
                    {
                        string[] columnChild1 = new string[] { root.Category_Name, root.Category_NameBM, root.Category_NameCH };
                        wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                        wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                        wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                        wt.Write(columnChild1[i]);
                        wt.RenderEndTag();//</a>

                        List<Category> childCategoryL2 = ctx.Database.SqlQuery<Category>
                            ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", root.CategoryID).ToList();

                        if (childCategoryL2.Count > 0)
                        {
                            wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                            foreach (Category child1 in childCategoryL2)
                            {
                                string[] columnChild2 = new string[] { child1.Category_Name, child1.Category_NameBM, child1.Category_NameCH };
                                wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                                wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                                wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                                wt.Write(columnChild2[i]);
                                wt.RenderEndTag();//</a>

                                List<Category> childCategoryL3 = ctx.Database.SqlQuery<Category>
                                    ("select CategoryId, category_name, category_nameBM, category_nameCH from dbo.lCategories where thread_parent = {0}", child1.CategoryID).ToList();

                                if (childCategoryL3.Count > 0)
                                {
                                    wt.RenderBeginTag(HtmlTextWriterTag.Ul);//<ul>

                                    foreach (Category child2 in childCategoryL3)
                                    {
                                        string[] columnChild3 = new string[] { child2.Category_Name, child2.Category_NameBM, child2.Category_NameCH };
                                        wt.RenderBeginTag(HtmlTextWriterTag.Li);//<li>
                                        wt.AddAttribute(HtmlTextWriterAttribute.Href, "#");
                                        wt.RenderBeginTag(HtmlTextWriterTag.A);//<a>
                                        wt.Write(columnChild3[i]);
                                        wt.RenderEndTag();//</a>
                                        wt.RenderEndTag();//</li>
                                    }
                                    wt.RenderEndTag();//</ul>
                                }
                                wt.RenderEndTag();//</li>
                            }

                            wt.RenderEndTag();//</ul>
                        }
                        wt.RenderEndTag();//</li>
                    }

                    wt.RenderEndTag();//</ul>
                    wt.RenderEndTag();//</li>
                }
            }


            string menuHTML = sWriter.ToString();

            string filePath = Path.Combine(strPath, @"Views\Shared\CacheData\CateMenu\");
            new FileInfo(filePath).Directory.Create(); //create new folder
            var menuPath = String.Format("{0}{1}", filePath, tempCateHTML[i]); //create multiple HTML files for multiple language

            using (FileStream fs = new FileStream(menuPath, FileMode.Append, FileAccess.Write))
            {
                StreamWriter sw = new StreamWriter(fs);
                sw.WriteLine(menuHTML);
                sw.Flush();
                sw.Close();
                fs.Close();
            }

            if (!File.Exists(filePath + cateHTML[i]))
            {
                using (File.Create(filePath + cateHTML[i]))
                {
                    //create dummy file if the file doesnt exists
                }
            }

            File.Replace(menuPath, filePath + cateHTML[i], filePath + cateHTML[i] + ".bac");
        }
    }

}

这是一个漫长的过程。非常感谢您的时间

1 个答案:

答案 0 :(得分:0)

实际已经找到了我的问题的解决方案。该解决方案基于this method构建了树状菜单。谢谢你的帮助。