ASP.NET MVC 是否要求使用母版页?
我的小组正在开发一个新的 Web 应用程序,并正在考虑使用 MVC。但是,有些成员宁愿包含页面也不愿使用母版页。这可以在 ASP.NET MVC 中实现吗?在我研究 MVC 的一小段时间里,我还无法弄清楚如何实现这一目标。
My group is working on a new web application and is considering using MVC. However, there are members who would rather include pages than use master pages. Is this possible to do in ASP.NET MVC? In the small amount of time that I've poked around with MVC I have not yet been able to figure out how one might accomplish that.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
为何偏爱?
过去使用过这两种方法,母版页更容易使用。你只需要克服(非常小的)学习曲线。
不过,ASP.NET MVC 并不强迫您执行任何一种操作...
如果您喜欢 Include 方法,那么您可能会觉得使用部分视图来提供相同的功能最舒服。您只需将部分视图添加到每个页面,而不是包含另一个页面。
Why the preference?
Having used both in the past, Master Pages are much easier to use. You just have to get over the (very small) learning curve.
ASP.NET MVC doesn't force you to do either one though...
If you like the Include method, then you would probably feel most comfortable using Partial Views to provide the same functionality. You would just add the Partial Views to each page instead of including another page.
不。它不会以任何方式强迫您。
No. It does not force you in any way.
您确实应该避免将服务器端包含到任何比经典 ASP 更新的内容中。它们更难调试,IIS 在出现问题时很难找到正确的行号,等等。此外,我还没有查看 SSI 在请求管道中处理的顺序 - 它们可能根本不起作用与 ASP.NET。
如果您要转向 MVC,请改用
RenderPartial()
或RenderAction()
。它们执行与服务器端包含基本相同的功能,但更符合框架的精神,并提供一些额外的好处,例如传递模型而无需声明全局变量(这也应该避免,我是不确定在 .NET 范围规则下是否可能)。而且,不,母版页不是必需的,但您确实应该使用它们。使用包含来构建页面布局是有效的,但前提是您在将来的任何时候都不需要也不需要从根本上更改网站的布局。我现在在这条船上有一个 350k 行的经典 ASP 应用程序,它使用非常好的结构化代码和 #includes 来创建页面布局。这是当时最好的解决方案,但现在(十多年后)却让我很头疼。
使用母版页,您可以将 ContentPlaceHolder 块移动到任何您想要的位置,而使用
#includes
最终页面实际上根据包含内容的放置顺序来确定格式。这也使得创建网站的移动版本变得非常简单 - 您可以创建特定于移动设备的母版页并使用相同的内容视图。You should really avoid server-side includes with anything newer than classic ASP. They're more difficult to debug, IIS has a hard time finding correct line numbers when there's a problem, etc. Also, I haven't looked at the order in which SSIs are processed in the request pipeline - they may not work at all with ASP.NET.
If you're moving into MVC, use
RenderPartial()
orRenderAction()
instead. These perform essentially the same function as a server-side include, but are more inline with the spirit of the framework and provide some additional benefits, like passing models without having to declare a global variable (which should also be avoided, and I'm not sure if it is even possible under .NET scope rules).And, no, master pages are not required, but you really should use them. Using includes to build your page layout works, but only if you don't and won't need to radically change the layout of your site at any point in the future. I'm in that boat now with a 350k line classic ASP app which used very nicely structured code and #includes to create the page layout. That was the best solution available at the time, but it's causing me a lot of headaches now (10+ years later).
With a master page you can move your ContentPlaceHolder blocks anywhere you want, whereas with
#includes
the final page really determines the format by the order in which the includes are placed. This also makes it pretty straightforward to create a mobile version of your site - you can create a mobile-specific master page and use the same content views.这是一个选择问题,但为了使 Web 应用程序具有一致的外观和感觉,母版页可以满足您的需求。您必须带领团队完成良好母版页设计的学习曲线,它不仅对当前的项目有用,而且对未来的项目有用。祝你好运!
Its a matter of choice,but for consistent look and feel across the web application, master pages give you just that. You have to take the team through the learning curve of good master page design, not only would it be useful for the current project at hand but also future projects. Good luck!
我宁愿选择母版页,因为它易于使用并且 MVC 对此有内置支持。
如果您想了解更多信息,请查看本教程: 使用视图母版页创建页面布局。
格兹,克里斯。
I would rather opt to go for Master pages due to the ease of use and built in support in MVC for this.
If you want to know more about it check out this tutorial: Creating Page Layouts with View Master Pages.
Grz, Kris.