ASP.Net MVC 框架是否有与 Monorail 视图组件等效的组件?
I make heavy use of View Components in some of the larger applications I've built in Monorail - What is the equivalent approach in ASP.Net MVC for a view component, that can support sections etc.?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
实际上,您有多种选择来创建 ASP.NET MVC 中的 ViewComponent 的等效项,具体取决于组件的复杂性。 我使用这两种方法,这是我所知道的更 mvc 风格的方法。
1:
最简单的事情是创建一个 ViewUserControl 并使用 Html.RenderPartial 和帮助程序显示它。 ViewUserControl 是一个简单的标记,没有后备控制器(我认为如果需要,您可以放置一个代码隐藏文件)。
或者,您可以在调用 RenderPartial 时将模型对象或整个 ViewData 字典传递给视图,如下所示:
“TopBar”是一个 ascx 页面。 这适用于任何地方,在母版页和普通视图中。
2:
如果您希望组件具有更复杂的逻辑或访问数据源、IoC 等,那么您可以使用 Html.RenderAction,它是 Microsoft.Web.Mvc 程序集中的扩展方法。 我正在使用 mvccontrib 发行版中的这个。 这样的,您需要创建一个包含您需要的所有逻辑的普通控制器,然后创建一些视图,所有这些东西都将成为您的组件,例如
:
它的工作原理是 注入了 IoC(在我的例子中是 Windsor),我可以做任何普通控制器会做的事情。
现在,在您想要使用组件的任何页面(主页面或普通页面)中,导入 Microsoft.Web.Mvc 并使用适当的参数调用 Html.RenderAction。 这将创建一个迷你 mvc 管道,用于创建控制器、解析视图等,就像 Monorail ViewComponent 一样。 我更喜欢使用基于 lambda 的方法变体,如下所示:
不幸的是,传递参数的唯一方法是使用方法调用本身,而方法调用本身在控制器中必须是唯一的。 仍然需要一些工作来类似于 ViewComponent。
我不在组件的视图中使用母版页或布局,因为它们本身就是组合元素。
请记住,在使用 Webforms 视图引擎时,如果您希望在代码块中使用 Model 变量时具有智能感知功能,则可以使用强类型视图。
这样做的好处在于,您可以将视图引擎与这些方法混合使用,我通常在 nvelocity 中创建组件并将它们显示在 aspx 页面等中。
我现在可能存在部分视图缓存的问题,但我还没有遇到过到目前为止任何。 我确信还有其他选项(例如 mvccontrib 中的子控制器),但这通常足以满足简单的情况。 当然,您可以在 aspx 视图页面中使用普通的 ASP.net 组件,但这会是作弊,对吗? 呵呵。 我希望它有帮助。
Actually you have several options to create the equivalent of a ViewComponent in ASP.NET MVC, depending in the complexity of your component. I use these two approaches which are the more mvc-ish of the options I am aware of.
1:
The simplest thing is to create a ViewUserControl and display it using Html.RenderPartial with the helper. The ViewUserControl is a simple piece of markup with no backing controller (I think you can put a codebehind file if you want).
Optionally, you can pass a model object or the entire ViewData dictionary to the view when calling RenderPartial, like this:
"TopBar" is an ascx page. This works anywhere, in master pages and in normal views.
2:
If you want your component to have more complicated logic or to access datasources, IoC, etc, then you can use Html.RenderAction which is an extension method found in the Microsoft.Web.Mvc assembly. I am using this out of the mvccontrib distribution. It works like this, you need to create a normal controller with all the logic you need, then create some views and all of these things become your component, for example:
}
Notice how I have a reference to an IRepository which is going to be injected with IoC (Windsor in my case) and I can do anything a normal controller would do.
Now, in any page (master or normal) where you want to use your component, import Microsoft.Web.Mvc and call Html.RenderAction with the appropriate parameters. This will create a mini mvc pipeline that creates the controller, resolves the view, etc., just like a Monorail ViewComponent. I prefer to use the lambda based variation of the method, like this:
Unfortunately, the only way to pass parameters is to use the method call itself, which in turn must be unique in the controller. Still needs some work to resemble a ViewComponent.
I don't use masterpages or layouts in the views of my components since they are composition elements themselves.
Remember that when using the Webforms view engine, you can have strongly typed views if you like to have intellisense when using the Model variable in code blocks.
The beauty of this is that you can mix view engines with these approaches, I usually create the components in nvelocity and display them in aspx pages, etc.
I now there can be issues with caching of the partial views but I haven't run into any so far. I am sure there are other options (like subcontrollers in mvccontrib) but this is usually enough for simple cases. Of course you can use normal ASP.net components in your aspx view pages but that would be cheating right? hehe. I hope it helps.
Phil Haack 博客介绍如何创建区域以将控制器分组到其中子文件夹/部分类似于 MonoRails。
Phil Haack blogged about creating areas to group controllers into sub-folders/sections similar to MonoRails.