3

In looking at the latest release of the Razor view engine with ASP.NET web pages, it looks nice because it has the full Razor support. So can someone give me a reasoning why I might want to choose ASP.NET Web Pages with the Razor view engine over traditional web forms, or its Razor view engine companion in ASP.NET MVC?

Per web pages, I'm speaking of this new web site framework available with VS 2010 SP 1: http://www.asp.net/webmatrix/tutorials/2-introduction-to-asp-net-web-programming-using-the-razor-syntax

Thanks.

Brian Mains
  • 50,520
  • 35
  • 148
  • 257

3 Answers3

3

The markup in razor is much more readable. When you have a product that already uses the web forms view engine, you can run razor side by side with it while you transition to razor views, which can be a lot of work in medium to larger sized projects. Keep in mind that during this transition, partials may be either razor or web forms, but a view and the master page it uses must use the same engine.

Nick Larsen
  • 18,631
  • 6
  • 67
  • 96
  • Thanks for the reminder about the master and web form using the same engine - I haven't run into that yet, but I wondered... :) – GalacticCowboy Jul 20 '11 at 12:44
  • Yes, I agree, I'm asking in the context of the new ASP.NET web pages framework with Razor, where web forms have the razor syntax... though apparently there's no code-behind. – Brian Mains Jul 20 '11 at 13:13
2

[Modified to answer the question posed in the title]

As an experienced developer, you would use the Web Pages framework when you want to get something done quickly (POC, for example), or if you want to teach someone how to get started with ASP.NET, perhaps.

You are asking two separate questions here: Web Forms View Engine vs Razor View Engine in MVC, and Web Pages Framework vs Web Forms Framework.

As others have said the first decision will be down to preference. Neither does anything that the other doesn't. It's a bit like the C# or VB debate.

The Web Pages framework is intended to provide an easier "in" for beginners than Web Forms or MVC. It can be considered a very belated replacement for the much easier classic ASP framework and it competes with PHP in that respect. If you need separation, testability and so on you should look to the MVC framework. The Web Pages framework doesn't offer very much at all in terms of enterprise development approaches.are used to Web Forms you are unlikely to gain anything from Web Pages. Separation is a lot more difficult. It doesn't encourage layering of code or testing. No server controls or code-behind files. And Web Pages only offers the Web Site deployment (compile on first run) as opposed to the Web Application which can be pre-compiled.

Razor itself was developed for the Web Pages framework. MVC liked it and leveraged it as a new view engine option. But essentially, you have three ASP.NET frameworks to choose from now: Web Pages, Web Forms and MVC.

Mike Brind
  • 28,238
  • 6
  • 56
  • 88
  • I'm not asking two separate questions, I'm asking Web Pages Framework with Razor view engine vs. MVC + Razor View Engine. – Brian Mains Jul 22 '11 at 12:17
  • Your question asks about Web Forms. You cannot use Razor with Web Forms. – Mike Brind Jul 22 '11 at 17:49
  • OK, so that's a typo, but the title states it all. – Brian Mains Jul 22 '11 at 18:56
  • 1
    And there was me thinking that the question said it all. – Mike Brind Jul 22 '11 at 19:52
  • 1
    @MikeBrind actually you can instantiate and execute Razor classes in a very similar way that you load an UserControl i WebForms, so its not really true that you can't use Razor in WebForms. And since Razor classes just outputs pure text its easy to insert where ever you like on your WebForm Page http://stackoverflow.com/questions/8129712/razor-views-in-asp-net-web-project/8129869#8129869 – Pauli Østerø Nov 15 '11 at 00:11
  • @PauliØsterø I know. I wrote an article about it some time ago: http://www.mikesdotnetting.com/Article/162/Using-Web-Pages-Helpers-in-ASP.NET-Web-Forms. To clarify - you cannot use Razor syntax directly in an aspx file. – Mike Brind Nov 15 '11 at 09:21
1

The main reasons to choose Razor:

  • Terse - The HTML structure of your page is obvious, and doesn't have lots of <% and %> everywhere. (I like clean HTML. It tickles my OCD...)
  • Fluent - Simple transitions between server-side code and HTML without forcing you to explicitly context-switch.
GalacticCowboy
  • 11,663
  • 2
  • 41
  • 66
  • I agree with your points. But now that ASP.NET web pages (not web forms) has razor syntax, would you use that over MVC's implementation? – Brian Mains Jul 20 '11 at 13:09
  • 2
    Sorry, I didn't realize that was the question. That's really a "WebForms" vs. "MVC" question since the view engine is only a small part of the total equation. My personal preference is for MVC, though that really has little to do with Razor. – GalacticCowboy Jul 20 '11 at 13:25