MVC ASP.NET or Razor

2020-05-25 07:22发布

问题:

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.

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.

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

回答1:

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.

You should read Scott Guthrie's blog post 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 %>

becomes

@Model.UserName

and

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

becomes

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

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



回答2:

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.



回答3:

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.

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

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



回答4:

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.