c# MVC5 加载菜单项的正确位置是什么
c# MVC5 What is the right place to load menu items
我有一个小问题,你能解释一下使用 MVC5 和 Entity Framework6 从数据库加载菜单项的最佳做法是什么吗?菜单和本地化对象必须只加载一次,然后从一些全局可用的集合中使用。它们在网站启动后不会有太大变化,所以我只是去实现一些 Update() 方法,我会在必要时调用它...
使用子操作。
public class FooController : Controller
{
...
[ChildActionOnly]
public ActionResult SiteMenu()
{
// load menu items however you need to
return PartialView("_SiteMenu", menuModel);
}
}
/Views/Shared/_SiteMenu.cshtml
@model Namespace.To.MenuModel
<!-- render your menu here -->
/Views/Shared/_Layout.cshtml
<!-- wherever you want the menu to display -->
@Html.Action("SiteMenu", "Foo")
如果您想缓存结果,这样菜单就不必在每次请求时都从数据库中提取,那么您可以像其他任何操作一样在子操作上使用 OutputCache
属性。
因为我已经难过了,所以我想过Global.asax
所以目前有两种方法可以使用 Global.asax:
Update 使用这种方法是个坏主意,改用第二种方法
public static ICollection<MenuItem> MenuItems {
get
{
if (Application["MenuItems"] != null)
return (ICollection<MenuItems>)Application["MenuItems"];
else
return new ICollection<MenuItems>();
}
set
{
Application["MenuItems"] = value;
}
}
private void LoadMenuItems()
{
MyContext mc = new MyContext();
this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
}
protected void Application_Start(object sender, EventArgs e)
{
this.MenuItems = LoadMenuItems();
}
另一种方式(第二种):
public static ICollection<MenuItem> MenuItems { get; set; }
private void LoadMenuItems()
{
MyContext mc = new MyContext();
this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
}
protected void Application_Start(object sender, EventArgs e)
{
this.MenuItems = LoadMenuItems();
}
本地化也一样...
其实我不知道哪个更好,需要运行一些测试。
差点忘了:
所有的东西,都包含在"CustomHttpApplication" class中,它来自"HttpApplication" class。并且 Global.asax 应该来自 "CustomHttpApplication" class。这样 Global.asax 文件将变得干净且可读,但业务逻辑将位于下一层...
所以完整的代码可能是这样的:
CustomHttpApplication.cs
public class CustomHttpApplication : HttpApplication
{
public static ICollection<MenuItem> MenuItems { get; set; }
private void LoadMenuItems()
{
MyContext mc = new MyContext();
this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
}
}
Global.asax.cs
public class MvcApplication : CustomHttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
MenuItems = this.LoadMenuItems();
}
}
再进行一次编辑,如果 you/me 将 "LoadMenuItems" 方法转换为 "static" 方法,那么 you/me 将能够更新 MenuItems and/or 需要时的本地化项目集合。
我有一个小问题,你能解释一下使用 MVC5 和 Entity Framework6 从数据库加载菜单项的最佳做法是什么吗?菜单和本地化对象必须只加载一次,然后从一些全局可用的集合中使用。它们在网站启动后不会有太大变化,所以我只是去实现一些 Update() 方法,我会在必要时调用它...
使用子操作。
public class FooController : Controller
{
...
[ChildActionOnly]
public ActionResult SiteMenu()
{
// load menu items however you need to
return PartialView("_SiteMenu", menuModel);
}
}
/Views/Shared/_SiteMenu.cshtml
@model Namespace.To.MenuModel
<!-- render your menu here -->
/Views/Shared/_Layout.cshtml
<!-- wherever you want the menu to display -->
@Html.Action("SiteMenu", "Foo")
如果您想缓存结果,这样菜单就不必在每次请求时都从数据库中提取,那么您可以像其他任何操作一样在子操作上使用 OutputCache
属性。
因为我已经难过了,所以我想过Global.asax 所以目前有两种方法可以使用 Global.asax:
Update 使用这种方法是个坏主意,改用第二种方法
public static ICollection<MenuItem> MenuItems {
get
{
if (Application["MenuItems"] != null)
return (ICollection<MenuItems>)Application["MenuItems"];
else
return new ICollection<MenuItems>();
}
set
{
Application["MenuItems"] = value;
}
}
private void LoadMenuItems()
{
MyContext mc = new MyContext();
this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
}
protected void Application_Start(object sender, EventArgs e)
{
this.MenuItems = LoadMenuItems();
}
另一种方式(第二种):
public static ICollection<MenuItem> MenuItems { get; set; }
private void LoadMenuItems()
{
MyContext mc = new MyContext();
this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
}
protected void Application_Start(object sender, EventArgs e)
{
this.MenuItems = LoadMenuItems();
}
本地化也一样...
其实我不知道哪个更好,需要运行一些测试。
差点忘了: 所有的东西,都包含在"CustomHttpApplication" class中,它来自"HttpApplication" class。并且 Global.asax 应该来自 "CustomHttpApplication" class。这样 Global.asax 文件将变得干净且可读,但业务逻辑将位于下一层...
所以完整的代码可能是这样的:
CustomHttpApplication.cs
public class CustomHttpApplication : HttpApplication
{
public static ICollection<MenuItem> MenuItems { get; set; }
private void LoadMenuItems()
{
MyContext mc = new MyContext();
this.MenuItems = ms.MenuItems.Include("SubCategories").AsNotTacking().where(x => x.SubCategory == null).ToArray();
}
}
Global.asax.cs
public class MvcApplication : CustomHttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
MenuItems = this.LoadMenuItems();
}
}
再进行一次编辑,如果 you/me 将 "LoadMenuItems" 方法转换为 "static" 方法,那么 you/me 将能够更新 MenuItems and/or 需要时的本地化项目集合。