17

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?

tereško
  • 58,060
  • 25
  • 98
  • 150
Dan P
  • 1,939
  • 3
  • 17
  • 30
  • Yeah I need a bit more of why vs 1 vs the other. What does one provide that the other doesn't provide. I'm I limiting myself to a an open source 3rd party implementation that may not be around in a few years if I use Razor etc? – Dan P May 08 '12 at 03:14
  • See [related SO thread](http://stackoverflow.com/q/4702231/304683) – EdSF May 08 '12 at 03:22

4 Answers4

21

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.

James McCormack
  • 9,217
  • 3
  • 47
  • 57
Jon Crowell
  • 21,695
  • 14
  • 89
  • 110
  • 1
    Here's another good resource that should give you comfort regarding whether Razor is going anywhere: http://www.dotnetrocks.com/default.aspx?showNum=606 (.NET Rocks episode with Phil Haack on Razor and MVC) – Jon Crowell May 08 '12 at 03:25
  • So in essence Razor shorhands the <%= blah %> with @blah? If this the case edit your answer and I will accept as all I see with this post is that is what it does. – Dan P May 08 '12 at 03:42
  • 1
    I already acepted answer but other than <%= name %> with @name is there any other major differences. Would I ever be advised in starting an app as an MVC "ASPX" engine as apposed to a "Razor" Engine? – Dan P May 08 '12 at 04:06
  • hi, I like to know can we write multi tier apps with razor – fc123 Oct 04 '14 at 11:41
4

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.

Chris
  • 27,596
  • 25
  • 124
  • 225
  • So in you're line of thought if "Razor" is an MS implementation of MVC it will more than likely succeed to the next version of VS? – Dan P May 08 '12 at 03:19
  • Razor is not a Visual Studio thing. It's a core piece of the ASP.NET MVC framework. Microsoft is releasing Razor V2 with ASP.NET MVC4, so no, it's not going anywhere. – Chris May 08 '12 at 03:21
  • Razor is Microsoft's response to it's ugly cousin, Spark(?). Web Forms aren't going away either but what does that prove? I have yet to see one single compelling argument to use Razor beyond the fact that it's the latest "trend". It opens the door to the same spaghetti we ran from with ASP 1.0 – IrishChieftain May 08 '12 at 03:38
  • @IrishChieftain can you expand on to why this will not help fix any of the problems with ASP 1.0? – Dan P May 08 '12 at 03:47
  • 1
    Code does not belong in the same place as markup - think maintenance. This is a fundamental rule of Web Development and I see no reason to go back on that for the sake of fashion. I don't see one solid argument for using Razor here so far. – IrishChieftain May 08 '12 at 03:55
1

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.

Matt Dawdy
  • 19,247
  • 18
  • 66
  • 91
  • My fear is that Razor is the latest and coolest technology that will be replaced in the next VS. I have been burnt before. If I am going to adapt the technology I need to know that it is going to be around and not replaced by a "MS Razor 2". What in particular does Razor offer over MS's MVC? – Dan P May 08 '12 at 03:17
  • Razor *IS* Microsoft technology. Also, Razor V2 is already scheduled to come out with ASP.NET MVC4, but there should be no backward compatibility issues - it's an extension of the existing base, not a new language syntax. – Chris May 08 '12 at 03:19
  • 1
    Dan P, I completely know where you are coming from. I've spent the first 7 years of my .Net life basically trying to avoid anything "built-in" because of how many times I got burned by MS's latest and greatest iteration of datagrids, or web paradigms, or foundation classes, etc. But these days, they really do seem to be nailing it with MVC. I do tend to shy away from the Entity Framework or Castle or anything else to do with DB abstraction. But the MVC thing appears to be dead on. – Matt Dawdy May 08 '12 at 03:23
  • Thanks and you caught my burnt detail dead on. I've been developer for 15 years. Silverlight was one of my biggest burns. I still think it's a great concept if Iphone and amazon's kindle fire were to bite on it...but it never became the all encompassed EVERYWHERE that windows promised. It seems things have shifted back to HTML and HTML5 which is why I am looking to reignite HTML and MVC knowledge. Thanks for feeling the burn with me :) – Dan P May 08 '12 at 03:31
-8

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.

IrishChieftain
  • 15,108
  • 7
  • 50
  • 91
  • 5
    Razor does not encourage combining code with markup any more than any other view engine. If you're under that impression, you're probably misinformed. The core concept in ASP.NET MVC is that if you want to extend the view with code-based logic, you write an HtmlHelper extension in a separate class file. – Chris May 08 '12 at 03:23
  • Every sample I've seen of it incorporates code into the view - big no-no. Should never have been allowed. This is such a contentious issue that we are sure to get some lively debate ;-) – IrishChieftain May 08 '12 at 03:25
  • 1
    @IrishCieftain I have very good knowledge of MVVM Silverlight and WPF. When switching to web I assumed MVC was the closest correlation. If you don't suggest MVC ASP.NET MVC or Razor what do you suggest? – Dan P May 08 '12 at 03:26
  • 1
    I suggest ASP.NET MVC with the regular parser. – IrishChieftain May 08 '12 at 03:27
  • 1
    Very bad suggestion, especially when your entire impression of Razor is based on every "sample I've seen", rather than personal experience. – Chris May 08 '12 at 03:29
  • 1
    So you recommend insertion of code snippets in markup? Honest question because I do not have personal experience. – IrishChieftain May 08 '12 at 03:31
  • 1
    @Chris was your comment to me or to IrishCieftain? I'm only now just going through the samples. I haven't made up my mind I'm just wondering if this code base is dead in two years or where it is going. – Dan P May 08 '12 at 03:39
  • 3
    My comment was @IrishChieftain. Razor does not endorse code in the view any more than Spark or the original ASP view engine does. In fact, Razor is an attempt to clean up the view markup. IrishChieftan's malice toward Razor is unfounded, as is his endorsement of the original ASP engine. – Chris May 08 '12 at 05:10
  • Just because it doesn't endorse it doesn't mean lots of people aren't doing it... take one look at the online samples these days. I suggest we address our opinions to the original OP since you can't come up with a solid reason to use it in the first place. Microsoft brought out Razor for a specific reason and it wasn't to clean up the markup. – IrishChieftain May 08 '12 at 05:14
  • 1
    @IrishChieftan hasn't the same always been said of CSS and JavaScript though? Best practices abound about separating markup from everything else, it's up to the coder to follow them. You can't blame the tool for the coder's bad decisions. – jonschlinkert May 13 '12 at 20:32
  • 1
    @jonschlinkert you have a point but would I still want to use it for the sake of saving a few key strokes, or am I missing something? – IrishChieftain May 13 '12 at 22:14
  • 2
    @IrishChieftan Your argument that every Razor sample you've seen incorporates code into the view is erroneous. If you see business logic in a View, that's the fault of the developer, not the View Engine. All tools can be abused. When Razor is used correctly, the resultant code is far quicker to write, and vastly more readable in comparison to the equivalent `<% .. %>` blocks. Having used both extensively, I can report there is no reason for using the original ASP view engine any more - it's inferior, period. – James McCormack Nov 29 '12 at 16:38
  • 1
    It's an open invitation for young Devs to do it wrong and the payoff it provides just don't justify it for me. A case of to each his own? :) – IrishChieftain Nov 29 '12 at 16:41
  • 1
    -1. "A fool with a tool is still a fool." Equating bad implementation decisions by developers to the usefulness of a development technology doesn't make any sense. – Ellesedil Apr 21 '16 at 00:26
  • 1
    Razor is a stupid decision by MS and brings us back 20 years in Web development. Fell free to downvote me. – IrishChieftain Apr 21 '16 at 13:40