简体   繁体   English

asp.net mvc布局逻辑的最佳位置在哪里

[英]Where is best place for asp.net mvc layouts logic

For example i have web site with heavy layout(masterpage). 例如,我的网站布局很重(主页)。 And for layout render we need to get some stuff from database and maybe do some calculations. 对于布局渲染,我们需要从数据库中获取一些东西并可能进行一些计算。

But where i must put this code? 但我必须把这段代码放在哪里?

  1. Put code in every view(maybe throught render section) is bad idea. 将代码放在每个视图中(可能是通过渲染部分)是个坏主意。
  2. Layout razor markup ofcourse too. 布局剃刀标记也是如此。 Markup is using for visualization logic but not for business logic. 标记用于可视化逻辑,但不用于业务逻辑。

The Haacked blogged about the Html.RenderAction helper (also refereed as child actions) which seem like a perfect fit for your scenario. Haacked在博客中介绍了Html.RenderAction助手(也称为子动作),它似乎非常适合您的场景。 It allows you to embed partials/widgets in your site that go through a separate Controller/Model/View lifecycle than the main one. 它允许您在站点中嵌入部分/小部件,这些部分/小部件通过单独的Controller / Model / View生命周期而不是主生命周期。 Very useful for incorporating common widgets in your Layout without the need of having your main controller worry about this transverse logic. 对于在布局中合并常见小部件非常有用,而无需让主控制器担心这种横向逻辑。

您应该将该逻辑放在子操作中并在布局中调用它们。

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

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