119

I would like to know what, why or when it is better to choose cshtml and what, why or when it is better to choose aspx technologies? What are these two technologies intended for?

Thank you,

Scott Solmer
  • 3,871
  • 6
  • 44
  • 72
Bastien Vandamme
  • 17,659
  • 30
  • 118
  • 200

4 Answers4

118

As other people have answered, .cshtml (or .vbhtml if that's your flavor) provides a handler-mapping to load the MVC engine. The .aspx extension simply loads the aspnet_isapi.dll that performs the compile and serves up web forms. The difference in the handler mapping is simply a method of allowing the two to co-exist on the same server allowing both MVC applications and WebForms applications to live under a common root.

This allows http://www.mydomain.com/MyMVCApplication to be valid and served with MVC rules along with http://www.mydomain.com/MyWebFormsApplication to be valid as a standard web form.

Edit:
As for the difference in the technologies, the MVC (Razor) templating framework is intended to return .Net pages to a more RESTful "web-based" platform of templated views separating the code logic between the model (business/data objects), the view (what the user sees) and the controllers (the connection between the two). The WebForms model (aspx) was an attempt by Microsoft to use complex javascript embedding to simulate a more stateful application similar to a WinForms application complete with events and a page lifecycle that would be capable of retaining its own state from page to page.

The choice to use one or the other is always going to be a contentious one because there are arguments for and against both systems. I for one like the simplicity in the MVC architecture (though routing is anything but simple) and the ease of the Razor syntax. I feel the WebForms architecture is just too heavy to be an effective web platform. That being said, there are a lot of instances where the WebForms framework provides a very succinct and usable model with a rich event structure that is well defined. It all boils down to the needs of the application and the preferences of those building it.

Joel Etherton
  • 37,325
  • 10
  • 89
  • 104
  • 11
    +1, That's an important point about the distinction between ASP.NET Web Form's aim of creating a stateful environment for the stateless web. – smartcaveman Jun 06 '12 at 12:07
  • One major advantage to aspx compared to cshtml is that you can view and edit the page itself (WUSIWYG kind of) using the design tab. With cshtml files you might as well use notepad to edit your html page. You are working "in the dark". – nivs1978 Aug 15 '18 at 13:20
  • @nivs1978: I actually find this to be a down-side of it. Too many developers use this as a crutch to write sloppy markup. In the end, the designer is only capable of showing you an "almost" version of the page since it will only render the view with its own internal "browser". While you may get a general sense of how a page will display, you still can't know until it's rendered in a true browser. – Joel Etherton Aug 16 '18 at 14:06
48

Razor is a view engine for ASP.NET MVC, and also a template engine. Razor code and ASP.NET inline code (code mixed with markup) both get compiled first and get turned into a temporary assembly before being executed. Thus, just like C# and VB.NET both compile to IL which makes them interchangable, Razor and Inline code are both interchangable.

Therefore, it's more a matter of style and interest. I'm more comfortable with razor, rather than ASP.NET inline code, that is, I prefer Razor (cshtml) pages to .aspx pages.

Imagine that you want to get a Human class, and render it. In cshtml files you write:

<div>Name is @Model.Name</div>

While in aspx files you write:

<div>Name is <%= Human.Name %></div>

As you can see, @ sign of razor makes mixing code and markup much easier.

Saeed Neamati
  • 35,341
  • 41
  • 136
  • 188
  • 6
    @MikaëlMayer, in Razor, `Model` is used for strongly typed views, in which you define your type at the top using `model` keyword. – Saeed Neamati Jul 04 '14 at 02:13
  • A small question for you @SaeedNeamati...... Say we are building a web application to consume complex micro-services based SOAP webAPI.... in this scenario, is asp.net mvc aspx or mvc cshtml is better to use. I personally felt cshtml syntax easier. – codemilan Nov 05 '17 at 09:37
  • Nice answer... this answer is much simpler to understand ...,at least for beginners like me :) – eRaisedToX Nov 28 '17 at 10:00
13

While the syntax is certainly different between Razor (.cshtml/.vbhtml) and WebForms (.aspx/.ascx), (Razor's being the more concise and modern of the two), nobody has mentioned that while both can be used as View Engines / Templating Engines, traditional ASP.NET Web Forms controls can be used on any .aspx or .ascx files, (even in cohesion with an MVC architecture).

This is relevant in situations where long standing solutions to a problem have been established and packaged into a pluggable component (e.g. a large-file uploading control) and you want to use it in an MVC site. With Razor, you can't do this. However, you can execute all of the same backend-processing that you would use with a traditional ASP.NET architecture with a Web Form view.

Furthermore, ASP.NET web forms views can have Code-Behind files, which allows embedding logic into a separate file that is compiled together with the view. While the software development community is growing to be see tightly coupled architectures and the Smart Client pattern as bad practice, it used to be the main way of doing things and is still very much possible with .aspx/.ascx files. Razor, intentionally, has no such quality.

smartcaveman
  • 41,281
  • 29
  • 127
  • 212
7

Cshtml files are the ones used by Razor and as stated as answer for this question, their main advantage is that they can be rendered inside unit tests. The various answers to this other topic will bring a lot of other interesting points.

Community
  • 1
  • 1
Timothée Bourguignon
  • 2,190
  • 3
  • 23
  • 39