简体   繁体   English

带有Initialize()的ASP.NET MVC基本控制器通过HTml.Action()多次执行

[英]ASP.NET MVC Base Controller with Initialize() gets executed multiple times with HTml.Action()

This is a question about best-practices in ASP.NET MVC 这是有关ASP.NET MVC中最佳实践的问题

I've created a website in MVC. 我已经在MVC中创建了一个网站。 Because every page has a menu, I thought I'd create a base controller class which all MVC Controllers in the project inherit from. 因为每个页面都有一个菜单,所以我想我要创建一个基础控制器类,项目中的所有MVC控制器都将从中继承。 In The Initialize() function of the base controller class I would then load my Menu. 然后,在基本控制器类的Initialize()函数中,加载菜单。 In that way, I could keep my logic for loading the menu in one place and have it executed automatically. 这样,我可以将加载菜单的逻辑保留在一个位置并自动执行。

Code (C#): 代码(C#):

public abstract class BaseController : System.Web.Mvc.Controller
{
    protected override void Initialize(System.Web.Routing.RequestContext requestContext)
    {
        //Load the menu:
        ViewBag.HeaderModel = LoadMenu();
    }
}

public class HomeController : BaseController
{
    public ActionResult Index()
    {
        //the menu is loaded by the base controller, so we can just return the view here
        return View();
    }
}

That works like a charm. 就像魅力一样。 Now here's the problem. 现在是问题所在。

In my View, I present a list of the five latest Articles on the website. 在我看来,我在网站上列出了五篇最新文章。 Since the Articles have their own logic and their own section on the website, I've created an ArticleController, which inherits from BaseController as well, with an action that displays a PartialResult with my five latest Articles. 由于文章在网站上具有自己的逻辑和部分,因此,我创建了一个ArticleController,它也继承自BaseController,并带有一个操作,该操作显示了我的五篇最新文章的PartialResult。

public class ArticlesController : BaseController
{
    public ActionResult DisplayLatestArticles()
    {
       var model = ... //abbreviated, this loads the latest articles
       return PartialView("LatestArticles", model);
    }
}

And the method is called like this in the View: 然后在View中这样调用该方法:

@Html.Action("Index", new { controller = "Articles" })

But this has one drawback: namely the Initialize() function on my Base Controller is executed twice, which loads the menu two times, which is undesirable (for performance reasons). 但这有一个缺点:即,我的基本控制器上的Initialize()函数执行了两次,两次加载了菜单,这是不希望的(出于性能原因)。 So far I haven't been able to find a way to avoid this behavior. 到目前为止,我还没有找到避免这种行为的方法。

What do you suggest in so far as refactoring this code? 关于重构此代码,您有何建议? I want to make sure my logic to load my menu stays somewhere so it gets called automatically, without me or any other developers on the project having to worry about it. 我想确保加载菜单的逻辑保持在某个地方,以便自动调用它,而我或项目中的任何其他开发人员都不必担心。 I prefer keeping my logic to display the latest Articles in the ArticlesController so everything to do with Articles is kept in its own Controller. 我更喜欢保持逻辑,以便在ArticlesController中显示最新的Articles,因此与Articles有关的所有事情都保留在其自己的Controller中。

So, how best to proceed? 那么,如何最好地进行?

What you're trying to do is more suited to calling your header menu from a _Layout.cshtml page. 您尝试做的事情更适合从_Layout.cshtml页面调用标题菜单。

_Layout.cshtml : _Layout.cshtml

<html>
...
  <body>
...
    @Html.Action("Header", "SharedStuff")
...

SharedStuffController.cs

public ActionResult Header()
{
    // logic to create header, also create a view
    return this.View();
}

Base controllers, I feel, are normally the wrong way to go. 我认为基本控制器通常是错误的方法。 The above means that you keep all logic for the header nicely contained in something that describes it. 上面的意思是您将标题的所有逻辑很好地包含在描述它的内容中。

I am not expert on the MVC and landed on this page while searching for my own answer. 我不是MVC专家,因此在搜索自己的答案时进入了此页面。 I completely agree that Base contoller should not be used for generating menu items. 我完全同意不应将Base contoller用于生成菜单项。 However, for what so ever reason, if you wanted to continue using base controller, I would suggest the controller that returns partial views (ie ArticlesController) should not inherit from base controller and it should inherit from Controller class. 但是,出于某种原因,如果您想继续使用基本控制器,我建议返回部分视图的控制器(即ArticlesController)不应继承基本控制器,而应该继承Controller类。

暂无
暂无

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

相关问题 如何从Html.Action中检索控制器和操作名称,该操作是从ASP.NET MVC中的不同控制器和操作呈现的 - How to retrieve controller and action names from Html.Action that is rendered from different controller and action in ASP.NET MVC ASP.NET MVC-发送空列表的HTML动作 - ASP.NET MVC - Html.Action Sending Empty List 在Asp.net MVC中使用Html.Action时发生异常 - Exception while using Html.Action in Asp.net mvc Asp.Net Core 中的@Html.Action - @Html.Action in Asp.Net Core 多次调用ASP.NET MVC后控制器操作 - ASP.NET MVC Post Controller Action Invoked multiple times Asp.net MVC3:真的基于角色@ Html.Action() - Asp.net MVC3: is really based on roles @Html.Action() 向服务器发送2或1个请求:在Asp.net Mvc中,在视图中使用“-@ Html.Action(””)-”向服务器发送2个请求 - 2 or 1 request to server: In Asp.net Mvc Does using “- @Html.Action(”“)-” in view makes 2 requests to server ASP.NET MVC Html.Action Render Post 而不是 GET - ASP.NET MVC Html.Action Render Post instead of GET 在asp.net mvc 4中,有没有一种方法可以在基本控制器中创建索引操作,并将该操作的视图共享给多个控制器? - is there a way in asp.net mvc 4 to create an Index Action inside Base Controller and Share the view of this Action to multiple Controllers? ASP.NET MVC以编程方式初始化Controller并调用操作 - ASP.NET MVC initialize Controller and call action programatically
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM