ASP.NET MVC 通用用户控件
我在普通 ASP.NET 中创建了一个通用用户控件:
通过一组属性,它从页面 A、B 或它恰好存在的任何父页面获取一堆元数据。
有时此用户控件有多个副本,当然每个副本在一个页面上使用不同的元数据。
有时在用户控件的代码隐藏中,根据其元数据,根据需要动态呈现子元素。
我现在正在研究 ASP.NET MVC。今天是此类活动的第一天,我不知道如何移植此用户控件的通用性。页面与控制器之间是否存在一对一的关系?用户控制怎么样?我的通用用户控件是否会直接链接到一个或多个控制器?如果是这样,这个链接是如何分配的?或者每个控制器只是将视图数据传递给用户控件?
谁能为我的场景提供高级设计思想?或者向我指出有用的链接?
I created a generic user control in vanilla ASP.NET:
Via a set property, it takes a bunch of metadata from page A, or B, or whatever parent page it happens to exist on.
There are sometimes multiple copies of this user control, each copy working off distinct metadata of course, on one page.
In code-behind the user control, based on its metadata, dynamically renders child elements as necessary.
I'm now looking into ASP.NET MVC. Today being the first day of such activity, I have no idea how the generic-ness of this user control could be ported over. Do pages have a one-to-one relationship with controllers? How about user controls? Would my generic user control link directly to one or more controller(s)? If so, how is this linking assigned? Or would each controller just pass view data to the user control?
Can anyone give me high-level design thoughts for my scenario? Or point me to helpful links?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
天哪...
Asp.Net mvc 的概念也与 RESTful 风格的网站结构密切相关。我不会费心再次解释 MVC,因为有一些很棒的文章,例如 http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
至于用户控制,整个概念是您不应该在所有页面上重复并提供相同的功能。相反,您可以创建一个部分视图,并使该部分视图将数据 POST 到特定的 URL。
然后,页面上有多个表单元素,所有元素都发布到特定的 URL,每个元素都提供页面该部分的功能。
EG 登录框
->带有登录名/密码输入的表单元素,该元素会发布到 /Account/Login
寄存器框
->一个带有名称/电子邮件/密码输入的表单元素,该元素会发布到/帐户/注册
等。
您确实需要放弃在 WebForm 中学到的很多东西才能开始使用 MVC。
编辑-我相信学习 MVC 的最佳方法是从一个小项目开始并开始。也是一个建议。 Asp.Net mvc 有很多你想要遵循的“约定”。
例如。默认页面位于 /home/index.html默认页面是/index.html文件夹命名结构。
当然你可以改变所有这些,但你通常只是在浪费时间。
Oh boy...
The concept of Asp.Net mvc also goes hand in hand with RESTful style website structure. I'm not going to bother with explaining MVC over again, since there are great articles such as http://weblogs.asp.net/scottgu/archive/2007/11/13/asp-net-mvc-framework-part-1.aspx
As for user control, the whole concept is that you shouldn't be duplicating and providing the same functionalility on all the pages. Instead, you'd create a partial view, and make that partial view POST data to a particular url.
Then you have multiple form elements on the page all posting to the specific URL that each provide the functionality for that part of the page.
E.G. A log in box
-> a form element with login/password input that post to /Account/Login
A register box
-> a form element with name/email/password input that post to /Account/Register
etc etc.
You really need to ditch a lot of stuff you learned in WebForm to get started with MVC.
Edit- I believe the best way to learn MVC is to get in there with a small project and get started. Also a piece of advice. Asp.Net mvc has a lot of "conventions" you want to follow.
Eg. default page at /home/index. default page is /index. the folder naming structure.
Granted you can change all those, but you are just wasting time usually.
实际上,只是为了给您指明方向,您可以查看以下内容:
通过自定义视图引擎从序列化源(例如sql/资源嵌入字符串)加载视图。
或者自定义 Html 帮助器扩展方法。
这两者之一和
自定义控制器工厂/激活器(在 MVC 网站之外或引用的 bin 文件中提供控制器 + 操作)
一种将事件挂钩到自定义用户控件的方法。我首先想到的是依赖注入。 (将服务注入到您打包为序列化部分视图的控制器的控制器操作中)
因此,为了使用“粒度、自包含”用户控件,您需要这些基础设施到位,并将控件打包在一种特殊的方式。
或者您可以在 MVC 中使用普通的旧用户控件。我认为这可行(谷歌搜索)
Actually, just to point you in a direction, you can look at the following:
Loading a View from serialised source (such as sql/resource embedded string) via custom view engine.
OR Custom Html helper extension method.
one of those two and
Custom Controller factory/activator (to serve up a controller + action outside of the MVC website or in a referenced bin file)
A way to hook events into your custom user control. First thing that cross my head is Dependency injection. (Injecting a service into the controller action you package up as the controller for the serialised partial view)
So, in order to use a "granular, self contained" user control, you'll need those infrastructure in place, and the control packaged in a special way.
Or you can just use plain old user control in MVC. I think that works (google it up)
一些让你滚动的想法。希望这有帮助。
在考虑 MVC 时,请将“视图”视为没有后台代码的传统“页面”。 “视图”具有控件和 HTML 以及其他渲染特定逻辑。
传统的代码背后被“模型”取代。控制器包含决定将模型的对象实例传递给“视图”的逻辑。
“控制器”不硬链接到任何视图。最好的方式是考虑一下。控制器可以处理多个“Action”。每个“Action”的处理代码可以返回一个“View”。这是呈现给用户的“视图”。
视图可以在控制器内以及控制器之间的多个操作之间共享。
您可以灵活地将视图分解为部分视图(类似于母版页/内容页概念)
)从这个角度来看,这是一种可能的继续方式。
获取几个 .aspx 页面并将它们更改为视图。同时尝试将代码隐藏中的代码重构为模型。您发现调用数据库并创建要呈现的数据对象的任何代码,将它们移动到控制器中。
以通用用户控件的简单情况为例,将其转换为视图,并使其作为部分视图与您转换为视图的上述 .aspx 页面一起工作。
通过练习,您应该能够很好地了解您的移植选项。
Couple of thoughts to get you rolling. Hope this helps.
In thinking about MVC, think of the "View" as the traditional "Page" without the code behind. The "View" has controls and HTML and other rendering specific logic.
The traditional code behind is replaced with a "Model". Controller contains the logic to decide with object instance of the model is passed to the "View"
"Controller"s are not hardlinked to any view. The best way to think about it. Controller can handle multiple "Action"s. The handling code for each "Action" can return a "View". This is the "View" that is rendered to the user.
A view can be shared across multiple actions within a controller and as well as across controllers.
You have the flexibility of decomposing your view into partial views (similar to master page/content page notion)
From that point of view, here is a possible way to proceed.
Take a couple of your .aspx pages and change them to views. While doing so try refactoring the code in codebehind into models. Any code you find that calls the database and creates data object to be rendered, move them into the controller.
Take a simple case of your generic user control and convert it to a view and get it working as a partial view with the above .aspx pages that you converted to views.
The excercise should give you good sense for what your porting options are.