Asp.net core 2中的View和Page有什么区别? [英] What is difference between View and Page in Asp.net core 2?

查看:161
本文介绍了Asp.net core 2中的View和Page有什么区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Asp.net核心2的新手.Asp.net核心2的新功能之一是页面. 但我无法弄清楚

  1. 页面和视图之间有什么区别?
  2. 页面与视图相比有什么好处?
  3. 在什么情况下应该使用页面?
  4. 我可以同时使用两者吗?

解决方案

真的,您的问题是Razor Page和传统MVC之间有什么区别.两者都利用Razor视图.剃刀页面是独立的(某种).它具有类似于Web窗体的代码背后,因此从技术上讲,您将有一个cshtmlcshtml.cs文件,后者将同时充当您的控制器和模型.而对于传统的MVC,您将有单独的FooControllerFooViewModelFoo.cshtml.除了一些细微的功能差异(例如必须在Razor Pages中使用基于惯例的操作",如OnGetAsyncOnPostAsync等)之外,基本上就可以了.

如我所见,Razor Pages的主要好处是其自成一体的性质.有关特定功能的所有代码基本上都集中在一个地方.但是,这样做的缺点是,这可能会使代码重用变得困难,或者至少在某些地方不那么直观.就我个人而言,我认为MVC提供的明确职责分工是更理想的方法,并且也不太神奇". Razor Pages的营销接触点之一是它非常简单.这可能是正确的,但是它的轻松"原因在于抽象化了Web开发人员实际上应该意识到的事情,这可能很危险.如果您实际上不了解某件事情的工作原理,那么您将不会知道自己做的事情是否正确.

我有偏见,但老实回答什么时候应该使用Razor Pages?",我不会说.我不喜欢职责混合,所有魔术"等等.自从引入这些问题以来,这里的ASP.NET Core问题数量猛增,并且大多数Razor Pages问题都是关于显而易见的或在使用MVC至少更直观.就是说,如果您使用它们,那么它们对于CRUD类型的东西是最有意义的-这些东西没有很多功能,而且很简单和/或重复./p>

最后,是的,您可以自由地混合和匹配Razor Pages和MVC.但是,应该指出这一点,因为它并不对所有人都显而易见:Razor Pages仅在用作Razor Pages时才用作Razor Pages.换句话说,如果您创建一个带有代码隐藏(Razor页面)的视图,然后将该视图用作MVC操作,部分代码等的返回,则实际上并没有利用代码隐藏查看"部分.实际上,更准确地说,它是使用的",但只是为了从一般意义上为视图提供模型,因为视图使用背后代码中的Page作为其模型.但是,这实际上不是活动的",在某种意义上说,不会点击页面操作,可能无法正确初始化的事情,等等.

I am new in Asp.net core 2. One of the new things in Asp.net core 2 are pages. But I can't figure it out

  1. What is difference between page and view?
  2. What are the benefits of pages versus views?
  3. In what cases should the page be used?
  4. Can I use both at the same time?

解决方案

Really, your question is what's the difference between a Razor Page and traditional MVC. Both utilize Razor views. A Razor Page is self-contained (sort of). It has a code-behind like a Web Form, so technically you'd have a cshtml and a cshtml.cs file, the latter of which would act as both your controller and your model. Whereas, with traditional MVC, you'd have separate FooController, FooViewModel, and Foo.cshtml. Aside from some slight functional differences (such as having to use convention-based "actions" like OnGetAsync, OnPostAsync, etc. with Razor Pages) that's pretty much it.

The chief benefit to Razor Pages, as I see it, is its self-contained nature. All the code regarding a particular piece of functionality is essentially all in one place. However, the downside to this is that it can make code reuse difficult or at least not as intuitive in some places. Personally, I think the clear division of responsibilities offered by MVC is the more ideal approach and its also less "magical". One of the marketing touchpoints of Razor Pages is that it's stupidly easy. That may be true, but it owes its "ease" to abstracting away things web developers should actually be cognizant of, and that can be dangerous. If you don't actually understand how something works, you won't know if you're doing things right.

I'm biased, but to honestly answer "When should Razor Pages be used?", I would say never. I don't like the mixing of responsibilities, all the "magic", etc. Since they were introduced the number of ASP.NET Core questions here have skyrocketed and the majority of the Razor Pages questions are about things that are either obvious or at least more intuitive with MVC. That said, if you are going to use them, they make the most sense on CRUD type stuff - things that don't have a lot of functionality and are fairly straight-forward and/or repetitive.

Finally, yes you can freely mix and match Razor Pages and MVC. However, it should be noted because it's not obvious to everyone: Razor Pages only work as Razor Pages when used as Razor Pages. In other words, if you create a view with a code-behind (a Razor Page) and then use that view as a return from an MVC action, a partial, etc., the code-behind isn't actually utilized, just the "view" portion. Actually, to be more accurate, it is "used" but only to provide the model for the view in a general sense, since the view uses the Page in the code-behind as its model. However, this won't actually be "active", in the sense that page actions won't be hit, things likely won't get initialized properly, etc.

这篇关于Asp.net core 2中的View和Page有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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