This is for improving knowledge of dot net technology, I have posted some articles related to Ajax, C sharp, vb.net, threading, linq, webservices, jQuery, Caching, .net Architecture, .Net Remoting, ADO.net, Authentication, ASP.net Overview, Debugger, Email integration, FTP, Payment Gateway, Performance and Scalability, Security, VS.net 2008, Web controls, Web Parts, WCF, WPF and SQL serve
Monday, May 16, 2011
Introducing MVC Development w/the Razor View Engine for Visual Studio Developers
The Razor View Engine is a precise, useful, light language that enables you to create views for MVC projects in ASP.NET still while keeping a separation of concerns, ability to test, and pattern based development. ASP.NET MVC developers looking for a more concise syntax now have another option that's built-in (of course, there are many 3rd party view engines out there) with the language being a familiar light version of C#.
The Razor View engine is used to create WebMatrix sites or Visual Studio MVC applications. When using ASP.NET MVC with either engine, you'll stick to a style of development called "convention over configuration", meaning that you'll use certain naming conventions to name files, models, views, controllers, and other key application elements rather than storing lots of metadata about these same elements in a configuration file. When using MVC in Visual Studio 2010, it's is setup so that you'll be guided to use convention over configuration, which becomes evident when exploring an ASP.NET MVC project.
Before you start, go and download these two things (as of this post; things are subject to change - these could end up in VS at some point):
You can also find the Razor Syntax Highlighter by choosing Extensions Manager from the Visual Studio Tools menu. Once installed, MVC 3 project templates are available from Visual Studio. The MVC 3 Web Application template allows you to use either the Web Forms View Engine or the Razor View Engine, while MVC 2 Applications contain only the WF View Engine.
When creating a new MVC 3 project a new dialog box appears asking which application type, view engine, and testing framework you'd like to use. You can andshould add a test project so you can test your code, then actually write some tests in it. The image below demonstrates selecting the internet application project template using Razor as the view engine, as well as the test framework. The internet application MVC template adds in ASP.NET membership & security features to the project by creating the necessary model, view and controller for logging on and registering as a site user.
Verify that the project is created with three folders, one each for Models, Views, and Controllers which is the same folder structure as a MVC 2 site. The project also contains auxiliary folders and files needed for the application such as the Content & App_Data folders. Since this is an MVC application using the Razor View Engine, you will see a different file extension - .cshtml. The .cshtml files are Razor View Pages written using the Razor View Engine. If you're not familiar with Razor syntax, I've blogged about it here, and the online documentation has more information as well.
The application template sets up some models, views and controllers to start with, and is now ready for new models, views, and controllers. I've found the easiest way to work with MVC in VS is to start with the model, move to the controller, then create the view, so we'll look at models first.
A model is a representation of an underlying data store. Models can be almost anything from any data source; EF or Linq2Sql Models, or a simple class. The code the builds the model below consists of two classes, a ProductModel class and a Product class. The ProductModel class returns a List of Product objects in a property aptly named Products that represents one or more products in the data store . Product objects contain ProductNumber, Name and Price properties and represent an individual product in the data store. The model code representing these product objects is below:
Models are handed over to views by controllers so the views can render the data from the model. The model will extend business logic (of course, this is a simple example, business logic will likely be spread out into other applications & tiers). Models also provide validation information to the view via metadata & code. You can add the model to the \Models folder or you can reference an external data model library. In this case, the model's been added to the \Models folder.