ASP.NET MVC2 - ViewPage 上的自定义属性

发布于 2024-09-28 03:28:12 字数 521 浏览 2 评论 0原文

我以为应该是一个相当简单的搜索,结果却是更多。

Atm 我正在为我的所有模型使用基类(MasterModel),然后从 ViewPage<<首页索引型号>到ViewMasterPage<主模型>一切正常。这是在阅读“Scott Gu”的帖子后这样做的。

然后我考虑从 ViewPage 继承并扩展 Factory 或 ViewPage 的构建位置......但后来我迷失了。

谁负责实例化ViewPage、ViewMasterPage 和ViewUserControl。

问题或多或少是我在 99% 的所有页面上使用同一类的实例,并且始终在 MasterPage 上,因此一直在模型中传递它是很痛苦的。

这是正确的方法吗?或者你们还有其他建议吗?

更新

我需要能够将从 IOC ( StructureMap ) 获取的复杂类型注入到 ViewPage 的构造函数中,这样就可以很容易地更改实现。这就是为什么我正在寻找构建 ViewPage 的地方。

What I thought should be a fairly simple search, turned out to be alot more.

Atm I'm using a baseclass(MasterModel) for all my Models, that then get passed down from the ViewPage< HomeIndexModel > to the ViewMasterPage< MasterModel > and everything works fine. This was done this way after reading a post by "Scott Gu".

Then I thought about inheriting from the ViewPage and extending the Factory or where ever the ViewPage is built from ... but then I got lost.

Who is responsible for instantiating the ViewPage, ViewMasterPage and ViewUserControl.

The problem is more or less that I'm using an instance of the same class on 99% of all pages and always on the MasterPage, so its a pain to keep passing it around in the model all the time.

Is this even the right way to go or do you guys have any other suggestions ?

Update

I need to be able to inject complex types taken from my IOC ( StructureMap ) into the constructor of the ViewPage, so it will be easy to change the implementation. Thats why I'm looking for the place where the ViewPage gets constructed.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

风蛊 2024-10-05 03:28:12

你能看看使用MVC3吗?依赖注入到视图页面是不可能的,因为创建被深深地埋藏在视图引擎的实现中。

现在 MVC3 中解决了这个问题。完整详细信息: http://bradwilson.typepad.com/博客/2010/07/service-location-pt3-views.html

Are you able to look at using MVC3? Dependency injection into view pages is not possible because the creation is buried deep inside the implementation of the view engine.

This is now addressed in MVC3. Full Details: http://bradwilson.typepad.com/blog/2010/07/service-location-pt3-views.html

拔了角的鹿 2024-10-05 03:28:12

据我了解,您有责任,因为在视图的定义中,您具体说明了视图的基类是什么。

试试这个:

public sealed class MyViewPage : System.Web.Mvc.ViewPage
{
  public string Herp {get{return "Derp";}}
}

并且,在你看来,这样做:

<%@ Page Language="C#" Inherits="MyApplication.MyViewPage" %>
<!-- compare the Inherits attribute in your standard View page -->
<%: Herp %>

我从来没有这样做过,但我 90% 确信它会起作用。您还需要做一个通用版本:

public sealed class MyViewPage<T> : System.Web.Mvc.ViewPage<T> {}

As I understand it, you are responsible, because within the definition of your View you specifically state what is the base class of your view.

Try this:

public sealed class MyViewPage : System.Web.Mvc.ViewPage
{
  public string Herp {get{return "Derp";}}
}

and, in your view, do this:

<%@ Page Language="C#" Inherits="MyApplication.MyViewPage" %>
<!-- compare the Inherits attribute in your standard View page -->
<%: Herp %>

I've never done this, but I'm 90% sure it would work. You would also need to do a generic version as well:

public sealed class MyViewPage<T> : System.Web.Mvc.ViewPage<T> {}
感情洁癖 2024-10-05 03:28:12

我会放弃基本视图模型和 DI 到 ViewPage 中,转而使用组合(通过 ViewData[]),因为它比视图模型的继承具有更大的灵活性。想象一下,如果您稍后想要向各个页面添加更多组件(标题、菜单、侧边栏、用户配置文件小部件等)。如果您必须将所有这些都放入您的基本视图模型中,那么它会变得拥挤,您甚至可能不需要每个视图上的所有内容。我纯粹将模型用于渲染的特定视图,然后将其他组件放入 ViewData 中。示例视图代码:

<% var headerModel = ViewData[Constants.HeaderData] as HeaderViewModel %>

或者超级简单的解决方案:您是否考虑过在视图中仅使用 ObjectFactory ? (这会让一些人——包括我自己——感到畏缩,但它确实有效。)

ObjectFactory.GetInstance<IService>();

I'd ditch the base view model and DI into ViewPage to use composition (via ViewData[]) instead because it allows you a lot more flexibility than inheritance of view models. Imagine if you later wanted to add more components (header, menus, sidebar, user profile widget, etc) to various pages. If you have to fit all that into your base view model it's going to get crowded and you probably won't even need everything on every view. I use the model purely for the specific view being rendered and then put my other components into ViewData. Sample view code:

<% var headerModel = ViewData[Constants.HeaderData] as HeaderViewModel %>

Or the super easy solution: have you considered just using ObjectFactory in your view? (This would make some people --myself included-- cringe, but it does work.)

ObjectFactory.GetInstance<IService>();
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文