ASP.NET Core Razor 页面与完整 MVC 核心 [英] ASP.NET Core Razor pages vs Full MVC Core

查看:27
本文介绍了ASP.NET Core Razor 页面与完整 MVC 核心的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SO 为什么 Razor Pages 是在 Asp.net Core 2.0 中创建 Web UI 的推荐方法? Steve Smith 在其中亲切地解释了使用 Razor Pages 而非完整 MVC 的好处从文件较少的角度来看.

There has been a question at SO Why is Razor Pages the recommended approach to create a Web UI in Asp.net Core 2.0? where Steve Smith has kindly explained the benefits of using Razor Pages over full MVC from the perspective of having less files.

我一直在使用 Razor Pages 并注意到尽管 Razor Pages 具有简单性的优点,但在自定义路由、结构化文件夹和复杂的视图模型(页面模型似乎杂乱无章)方面有点复杂).

I've been using Razor Pages for a while and noticed that despite an advantage of a Razor Page simplicity, it is a bit complicated when it comes to custom routing, structuring folders and complex view model (page model seem to be cluttered).

所以,问题是:

  1. 如果除了页面的简单性之外,还有什么比控制器/视图更喜欢 Razor 页面 - 特别是我对这两个框架的性能感兴趣?
  2. 是否可以同时组合 Razor 页面和控制器/视图?

如果有经验丰富的人分享您关于使用 Razor Pages 更好地理解此框架的想法(优点和缺点),我也将不胜感激.

I would also appreciate if some of expirienced guys shared your thoughts (pros and cons) about using Razor Pages to better understand this framework.

推荐答案

我们最近推出了一个相当不错的应用程序,它使用 Razor Pages 作为前端,使用 MVC 控制器作为 API 的客户端组件.我的经历是这样的:

We've recently launched a pretty decent sized app using Razor Pages for the front end and MVC controllers for the API for client side components. My experience has been this:

当您的内容围绕网站上的实际页面"的概念构建时,页面范例非常有效.想想诸如联系我们"或关于"甚至登录"页面之类的东西.当然,这些可以通过 MVC 完成,但 MVC 确实没有必要.一个简单的页面就足够了.将控制器留给更多控制器的东西,例如产品目录或用户数据库.

The pages paradigm works well when your content is structured around the idea of actual "pages" on the site. Think about things such as a Contact Us or an About or even a Login page. Sure, those could be done via MVC, but MVC is really unnecessary. A simple page will suffice. Leave the controllers to more controller'ish things like a product catalog or a user database.

如果您的 MVC 架构主要围绕您的视图结构,那么剃刀页面可能是一个不错的选择.您仍然可以将 MVC 位用于 API 相关的东西,但页面的好处是您的前端结构变得更加明确和不那么隐含(基于约定"),就像 MVC 一样,其中每个操作都可以或不可以有一个视图通常以动作命名.

If your MVC architecture revolves heavily around your view structure, razor pages is probably a good fit. You can still use the MVC bits for API related stuff, but the benefit of pages is that your front end structure becomes more explicit and less implicit ("convention-based") like with MVC where each action could or could not have a view that is typically named after the action.

这篇关于ASP.NET Core Razor 页面与完整 MVC 核心的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆