ASP .NET MVC正确的UserControl体系结构 [英] ASP .NET MVC correct UserControl architecture

查看:86
本文介绍了ASP .NET MVC正确的UserControl体系结构的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试学习新的ASP .NET MVC框架,并想了解使用UserControls的最佳实践.

I'm trying to learn the new ASP .NET MVC framework and would like to know the best practice for using UserControls.

我了解您可以将UserControl呈现为部分控件,并将数据从控制器传递给它们.理想情况下,我认为在文件后没有代码是有道理的,因为这会产生破坏MVC规则的诱惑.

I understand that you can render UserControl's as a partial and pass data to them from a controller. Ideally I would think that it makes sense not to have a code behind file as this creates a temptation to break the MVC rules.

我将举一个不了解UserControl如何适应模式的示例.

I'll give an example where I don't understand how UserControls fit into the pattern.

我有一个UserControl可以显示 最新标签(类似于 堆栈溢出).与StackOverflow我不同 想要在显示此UserControl 我所有的页面.如果我有一个 控制器说QuestionController 旨在处理来自 一些问题的看法,例如查看和 细节,这是否意味着我必须获取 QuestionController中的数据和 然后将其传递给UserControl?

I have a UserControl that shows the latest tags (much like on StackOverflow). Unlike StackOverflow I want to display this UserControl on all of my pages. If I have a controller say QuestionController which is meant to handle actions from some question views e.g. view and detail, does this mean I have to fetch the data in the QuestionController and then pass it to the UserControl?

如果我创建另一个控制器,请说 SearchController,那么我将不得不 复制相同的功能到 获取最新的标签以传递给 再次偏.这不是说 两个不同的控制器正在做 他们不是多余的东西 本来打算做的?

If I create another controller say SearchController then I would have to replicate the same functionality to get the latest tags to pass to a partial again. Doesn't this mean that the 2 different controllers are doing extra things that they weren't originally intended to do?

推荐答案

如果您的UserControl出现在每个页面上,则解决此问题的一种方法是使用基本控制器,您的所有控制器都将从该基本控制器派生并生成ViewData通过重写OnActionExecuting方法并在其中放置逻辑来控制UserControl.如果您的UserControl普及程度较低,但仍在整个站点中经常使用,则可以扩展ActionFilterAttribute并让您的过滤器生成所需的数据.此属性可用于装饰生成使用UserControl的视图的控制器或操作.

If your UserControl appears on every page, then one way to address this would be to use a base controller from which all of your controllers derive and generate the ViewData for the UserControl by overriding the OnActionExecuting method and putting the logic there. If your UserControl is less pervasive, but still frequently used throughout the site, you could extend ActionFilterAttribute and have your filter generate the needed data. This attribute could be used to decorate the controllers or actions that generate views that use the UserControl.

在所有这些中,我假设UserControl的数据独立于所调用的动作.如果存在依赖关系,最好将逻辑推入一个类(或多个类,也许使用Strategy),并在每个动作或控制器中明确显示数据的生成(通过覆盖OnActionExecuting).

I'm assuming in all of this that the data for the UserControl is independent of the action being invoked. If there is a dependency, it's probably best to push the logic into a class (or classes, perhaps using Strategy) and make the generation of the data explicit in each action or controller (via overriding OnActionExecuting).

这篇关于ASP .NET MVC正确的UserControl体系结构的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆