asp.net-mvc MVC ASP.NET 或 Razor

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10492022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 01:55:45  来源:igfitidea点击:

MVC ASP.NET or Razor

asp.net-mvcasp.net-mvc-3razor

提问by Dan P

I'm very new to MVC...I have quite a bit of knowledge with Silver-light and WPF and MVVM but little knowledge in regards to MVC. I'm following along the main tutorial on Microsoft's site http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/adding-a-view. I believe I'm on article 3 of 9. Originally it told me to create my MVC Site as using the "View Engine" of "Razor". I chose to use "ASPX" instead of "Razor" as I thought I would have less third party dependencies.

我对 MVC 很陌生……我对 Silver-light、WPF 和 MVVM 有很多了解,但对 MVC 知之甚少。我正在关注 Microsoft 网站http://www.asp.net/mvc/tutorials/getting-started-with-aspnet-mvc3/cs/adding-a-view上的主要教程。我相信我在 9 条中的第 3 条上。最初它告诉我使用“Razor”的“视图引擎”创建我的 MVC 站点。我选择使用“ASPX”而不是“Razor”,因为我认为我对第三方的依赖会更少。

When starting a new MVC Web Application is it best to use "Razor" as opposed to "ASPX". To be honest I have no idea of the difference. As stated I chose ASPX to avoid any extra third party dlls or code. If it is best to choose ASPX I'm a bit confused as to why the tutorials on MS's site are telling me to start with Razor.

启动新的 MVC Web 应用程序时,最好使用“Razor”而不是“ASPX”。老实说,我不知道有什么区别。如前所述,我选择 ASPX 是为了避免任何额外的第三方 dll 或代码。如果最好选择 ASPX,我有点困惑为什么 MS 网站上的教程告诉我从 Razor 开始。

Can anyone shed some light on to when I should use the "ASPX" view engine as opposed to the "Razor" view engine?

任何人都可以说明什么时候我应该使用“ASPX”视图引擎而不是“Razor”视图引擎?

回答by Jon Crowell

There is no difference regarding dependencies on 3rd party anything. ASPX is fine, but Razor is better, mostly because it stays out of your way.

对 3rd 方的依赖没有区别。ASPX 很好,但 Razor 更好,主要是因为它不会妨碍您。

You should read Scott Guthrie's blog post Introducing "Razor".

您应该阅读 Scott Guthrie 的博客文章Introducing "Razor"

You basically replace the opening and closing tags <%and %>with an @symbol, so far fewer keystrokes to do the same thing, i.e.

你基本上取代打开和关闭标签<%,并%>@符号,至今较少的按键做同样的事情,即

<%: Model.UserName %>

<%: Model.UserName %>

becomes

变成

@Model.UserName

@Model.UserName

and

<% foreach (string name in nameList) { .. } %>

<% foreach (string name in nameList) { .. } %>

becomes

变成

@foreach (string name in nameList) { .. }

@foreach (string name in nameList) { .. }

There's a little more to it than that, but not much.

还有更多的东西,但不多。

回答by Chris

Razor is a Microsoft internal extension to ASP.NET MVC. You seem to be under the impression that Razor is provided by the community or is an off-shoot of the MVC base components, which is entirely false. Razor also provides syntactically cleaner code than ASPX.

Razor 是 Microsoft 对 ASP.NET MVC 的内部扩展。您似乎认为 Razor 是由社区提供的,或者是 MVC 基础组件的一个分支,这完全是错误的。Razor 还提供比 ASPX 在语法上更简洁的代码。

回答by Matt Dawdy

The "extra third party dlls or code" you would be relying on are well established and absolutely no brainers to include. You copy it once and forget about it. It's not like you have to hunt them down, either. It's very easy Xcopy deployment.

您将依赖的“额外的第三方 dll 或代码”已经很好地建立起来,绝对没有任何脑筋急转弯要包括在内。你复制一次就忘记了。你也不必追捕他们。Xcopy 部署非常简单。

As you have seen, the MS MVC site uses Razor. Most (all?) of the sites and books I read are using Razor.

如您所见,MS MVC 站点使用 Razor。我阅读的大多数(全部?)网站和书籍都在使用 Razor。

Use Razor, and you'll find a lot more help than with aspx in MVC.

使用 Razor,您会发现比使用 MVC 中的 aspx 多得多的帮助。

回答by IrishChieftain

Razor is the sloppiest possible thing you could do to your markup. Code and markup do not belong in the same file, period. It is totally beyond me how anyone could recommend such madness.

Razor 是您可以对标记做的最草率的事情。代码和标记不属于同一个文件,句号。我完全无法理解如何有人推荐这种疯狂。