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

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

问题描述

SO出现了一个问题

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 Page简单性有优势,但是在自定义路由,结构化文件夹和复杂的视图模型方面还是有点复杂(页面模型看起来很杂乱) ).

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页面和Controllers/Views是否可以接受?

如果一些经验丰富的人分享了有关使用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页面,客户端组件的API使用MVC控制器.我的经验是这样的:

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 Core的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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