Developing with MVC/Razor

Visual Studio supports an alternative to building your application using Web Forms. This alternative is based on the Model-View-Controller (MVC) design pattern and the Razor syntax. The purpose of this pattern is to separate the application’s logic and data (model), its user interface display (view), and the code that helps the user interact with the UI and the data (controller).

Developers choose MVC because it gives them direct access to the HTML and CSS, allowing them to more easily work with responsive design and JavaScript frameworks. MVC also better supports test-driven development because the views are just markup, and all their logic is in controller classes that can be tested independently of the view markup.

You create a new MVC site by first selecting ASP.NET Web Application from the New Project dialog. This launches the unified New ASP.NET Project dialog. From here you can select Web Forms, MVC, Single Page Application, Web API, and more. Of course for MVC, select the template titled “MVC.” Figure 1.10 shows an example of the ASP.NET MVC site template inside Visual Studio. See Chapter 17 for a detailed discussion of ASP.NET MVC.

Image

FIGURE 1.10 An ASP.NET MVC project structure (see Solution Explorer) and simple HTML markup (see AddPhoneNumber.cshtml).


Note

Project templates do not preclude you from writing any type of ASP.NET code you like. That is, you can mix MVC with Web Forms, Web Pages with Razor, SPA, or any combination that makes your website work the way that makes sense to you. In fact, the New ASP.NET Project dialog now allows you to create one type of web application (such as MVC) but choose the references and core folders required of another (such as Web Forms and Web API).



Note

Visual Studio allows developers to also create websites built on just the Razor syntax. This is helpful for simple websites that do not need Web Forms or MVC but can take advantage of the easier syntax of working with HTML. It can serve as a nice basis for SPA applications. (See the next section.)


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset