如果MVC2使用aspx页面和母版页可以使用asp.net服务器控件吗?
我注意到 MVC 2 使用母版页和 ascx 页面。我习惯使用 Razor 页面,但必须处理较旧的项目。所以我想知道在这些 aspx 标记页面上使用 asp.net 服务器控件是否可以?
我知道建议不要插入页面生命周期,但是用户控件呢?
Ive noticed that MVC 2 uses masterpages and ascx pages. I'm used to using Razor pages but have to work on an older project. So I'm wondering if it is okay to use asp.net server controls on these aspx markup pages?
I know its advised not to plug into the page lifecycle but what about user controls?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想使用用户控件,请坚持使用 Web 表单。用户控件不与 MVC 一起使用。 MVC 视图引擎没有像 Web 窗体那样的页面生命周期。
If you want to use user controls, stick with Web Forms. User controls are not used with MVC. The MVC view engine does not have a page lifecycle like Web Forms.
您的用户控件插入到 WebForms 页面生命周期中,它们通过 ViewState 维护其状态。创建了一些帮助程序来将控件呈现给 MVC,但它们的局限性在于它们是只读的,控件不支持回发或任何类似的事件。
http ://malvinly.com/2011/02/28/using-web-forms-user-controls-in-an-asp-net-mvc-project/
您可以创建自己的部分控件:
在 MVC 项目中的 webform 上使用 webform 用户控件
Your Usercontrols plug in to the WebForms page lifecycle, they maintain their state through ViewState. There are helpers that were created to render out controls to MVC but they are limited in the fact that they are read-only, the controls don't support posting back or any events like that.
http://malvinly.com/2011/02/28/using-web-forms-user-controls-in-an-asp-net-mvc-project/
You could create partials out of your own controls:
Using webform user control on webform in MVC Project