mvc courses. Creating an engine on MVC

Audience profile:

  • The course is designed for professional web developers who use Microsoft Visual Studio individually or as a team, in organizations large or small.

Prerequisites:

  • 2-3 years of web application development experience using Microsoft Visual Studio and Microsoft ASP.NET;
  • hands-on experience with the .NET Framework;
  • Basic knowledge of the C# programming language.

Upon completion of the course, students will be able to:

  • Describe Microsoft web technologies and select the appropriate tool for the task at hand;
  • design the architecture and implementation of a web application that will satisfy a set of functional requirements, user interface requirements and business model;
  • create an MVC Model and write code that implements the business logic of the application and work with data;
  • add Controllers to MVC applications to manage user interaction, update the Model, and provide interaction with Views;
  • create MVC Views that allow you to display and edit data and interact with Models and Controllers;
  • run unit tests and debugging tools for web applications in Visual Studio 2012 and configure applications for troubleshooting;
  • develop web applications that use ASP.NET routing to provide friendly URLs and logical navigation for users;
  • ensure a consistent look and feel of site pages, including corporate branding, throughout an MVC web application;
  • use partial page refresh and caching to reduce the network load created by the application and reduce the response time to user actions;
  • write JavaScript code that runs on the client side and uses the jQuery scripting library to optimize the interaction of MVC web applications with the user;
  • create MVC applications that are resistant to malicious attacks and save information about user settings;
  • understand how to write a Windows Azure web service and access it from an MVC application;
  • describe what a Web API is and why developers can add it to an application;
  • change how the MVC application handles browser requests;
  • describe how to package and deploy an ASP.NET MVC 5 web application from a development machine to a web server;
  • development of web applications using ASP.NET MVC 5;
  • development of ASP.NET MVC 5 Models;
  • development of ASP.NET MVC 5 Controllers;
  • developing ASP.NET MVC 5 Views;
  • how to test and debug ASP.NET MVC 5 web applications;
  • structuring ASP.NET MVC 5 web applications;
  • feature of applying styles to ASP.NET MVC 5 web applications;
  • mechanism for building responsive pages in ASP.NET MVC 5;
  • the peculiarity of using JavaScript and jQuery to build responsive MVC 5 web applications;
  • principles of access control to ASP.NET MVC 5 web applications;
  • how to build robust ASP.NET MVC 5 web applications;
  • implementation of Web APIs in ASP.NET MVC 5 web applications;
  • request processing in ASP.NET MVC 5 web applications;
  • principles for deploying ASP.NET MVC 5 web applications.
The structure of the program on the example of the simplest program

MVC ASP .NET for dummies. Lesson 1 . Program "Hello, World!"

The source code for the lesson can be downloaded.

We will learn MVC from the very basics. To begin with, let's create a simple application "Hello, World!" And so, we bake Microsoft Visual Studio 2010 (it can run another one, then the pictures may not be the same as shown in the screenshots). Go to "File" -> "New" -> "Project":

Select project type "Visual C#" -> "ASP.NET MVC 2 Web Application":

After clicking on the "OK" button, Visual Studio will create a program template:

Let's remove all unnecessary:

  • App_Data folder.
  • Remove AccountController.cs from the Controllers folder
  • From the View folder, the Account folder.
  • The View folder contains the Home and Shared folders. Let's delete all the contents of these folders. Let's leave the folders.

This is what our tree will look like now:

Now we remove all unnecessary from the program code, in particular, the About() method:

And here is the Index method, we will rewrite it a bit. In particular, we will make its type string, and change the body of the method itself. This is how the whole HomeController.cs program will look after that:

using System;

using System.Collections.Generic;

using System.Linq;

using System.Web;

using System.Web.Mvc;

namespace MvcApplication2.Controllers

public class HomeController : Controller

public string Index()

return "Hello World!" ;

And this is what the program will produce when we compile it and run it.

Developing ASP.NET Core MVC Web Applications

The course is read according to the latest version!

During the course, you will learn how to create web applications using ASP.NET Core MVC from .NET Framework. You will gain knowledge and skills that will allow you to significantly improve the performance and scalability of your web applications. During the course, you will compare ASP.NET CORE MVC and ASP.NET Web Forms technologies and get recommendations for choosing one or another technology.

The course is calculated for professional web developers with experience in creating applications using Visual Studio.

To take this course, you need a valid account accessMicrosoft Azure. Please note that it may take up to 48 hours to get a new access. If you do not have access, please inform your manager when registering for training. In this case, we will provide you with a trial version: 30-day Windows Azure Pass.

Do you need enhanced practice? Preparing for Microsoft Certification Exams? Get access to Labs Online– virtual labs on authorized Microsoft courses – throughout the course and two weeks after graduation! The service is unique and available only in the Center "Specialist"

The concept of MVC (Model-View-Controller) has been mentioned a lot in the world of web programming in recent years. Everyone who is in any way connected with the development of web applications, one way or another, has come across this acronym. Today we will understand what the MVC concept is and why it has become popular.

ancient history

MVC is not a design pattern, it is a design pattern that describes the way our application is structured, the responsibilities and the interaction of each of the parts in this structure.

It was first described in 1979, of course, for a different environment. Then there was no concept of a web application. Tim Berners Lee planted the seeds of the World Wide Web (WWW) in the early nineties and changed the world forever. The template we use today is an adaptation of the original template for web development.

The frenzied popularity of this framework in web applications has developed due to its inclusion in two development environments that have become very popular: Struts and Ruby on Rails. These two development environments charted the paths for hundreds of production environments created later.

MVC for web applications

The idea behind the MVC design pattern is very simple: we need to clearly separate the responsibility for different behaviors in our applications:

The application is divided into three main components, each of which is responsible for different tasks. Let's take a look at the components in detail with an example.

Controller

Controller manages user requests (received as HTTP GET or POST requests when the user clicks on interface elements to perform various actions). Its main function is to call and coordinate the necessary resources and objects needed to perform user-defined actions. Typically, the controller calls the appropriate model for the task and selects the appropriate view.

Model

Model is the data and rules that are used to work with the data that represent the application management concept. In any application, the entire structure is modeled as data that is processed in a certain way. What is a user for an application - a message or a book? Only data that must be processed in accordance with the rules (the date cannot be in the future, the e-mail must be in a certain format, the name cannot be longer than X characters, and so on).

The model gives the controller a representation of the data that the user has requested (message, book page, photo album, etc.). The data model will be the same no matter how we want to present it to the user. Therefore, we choose any available view to display the data.

The model contains the most important part of our application logic, the logic that solves the problem we are dealing with (forum, shop, bank, etc.). The controller contains mostly organizational logic for the application itself (much like housekeeping).

View

View provides various ways to represent the data that is received from the model. It can be a template that is filled with data. There can be several different views, and the controller chooses which one best suits the current situation.

A web application usually consists of a set of controllers, models, and views. A controller can be set up as a master controller that receives all requests and calls other controllers to perform actions depending on the situation.

Let's take an example

Suppose we need to develop an online bookstore. The user can perform the following actions: view books, register, buy, add items to the current order, create or delete books (if he is an administrator). Let's see what happens when the user clicks on a category fantasy to view the titles of books that are available in our store.

We have a specific controller to handle all actions related to books (view, edit, create, and so on). Let's call it books_controller.php in our example. Also we need a model, for example, book_model.php An that handles the data and logic associated with the store item. Finally, we need several views to represent the data, such as a list of books, an edit page, and so on.

The following figure shows how a user's request to view a list of related books is handled. fantasy:

The controller (books_controller.php) receives the user's request (an HTTP GET or POST request). We can set up a central controller like index.php that receives the request and calls books_controller.php.

The controller checks the request and parameters and then calls the model(book_model.php), inquiring she has a list of available books on the topic fantasy .

The model gets the data from the database (or from another source that stores information), applies the filters and necessary logic, and then returns the data that represents the list of books.

The controller uses the appropriate view to present data to the user. If the request comes from a mobile phone, the mobile view is used; if the user uses a certain interface design, then the appropriate view is selected, and so on.

What are the benefits?

The most obvious advantage that we get from using the MVC concept is a clear separation of presentation logic (user interface) and application logic.

Support for different types of users who use different types of devices is a common problem these days. The interface provided should be different if the request comes from a PC or a mobile phone. The model returns the same data, the only difference is that the controller chooses different views to display the data.

In addition to isolating views from application logic, the MVC concept greatly reduces the complexity of large applications. The code is much more structured, making it easier to maintain, test, and reuse solutions.

Why use a workbench?

When you use a workbench, the basic MVC structure is already in place, and all you have to do is extend the structure by placing your files in the appropriate directories to match the MVC pattern. In addition, you will have a set of functions that are already written and well tested.

Let's take cakePHP as an example of an MVC workbench. After installation, you will have three main directories:

  • cake/
  • vendors/

Folder app is the location of your files. This is the place to develop your part of the application.

In folder cake hosts cakePHP files (workbench functionality).

Folder vendors serves to store third-party PHP libraries.

Your workspace (app directory) has the following structure:

  • app/
    • config/
    • controllers/
    • locale/
    • models/
    • plugins/
    • tests/
    • vendors/
    • views/
    • webroot/

You need to place your controllers in a directory controllers, models in directory models and views in the directory views!

As soon as you start using the workbench, it becomes immediately clear where almost any part of your application that needs to be created or modified is located. This organization in itself greatly simplifies the process of developing and maintaining an application.

Using the workspace for our example

Since this lesson is not intended to show how to create an application with cakePHP, we will only show the code for the model, controller, and view, with comments on the benefits of using the MVC workbench. The code is deliberately simplified and unsuitable for use in a real application.

Remember, we were looking at a bookstore and a curious user who wanted to see a complete list of books on the topic. fantasy. The controller received the user's request and coordinated the necessary actions.

So, as soon as the user clicks the button, the browser requests the given url:

www.ourstore.com/books/list/fantasy

CakePHP template URL formatting /controller/action/param1/param2, where action is a function that is called by the controller. In the old classic form, the url will look like this:

www.ourstore.com/books_controller.php?action=list&category=fantasy

Controller

In the cakePHP workspace, our controller will look like this:

class BooksController extends AppController(

Function list($category) (

$this->set("books", $this->Book->findAllByCategory($category));

function add() ( ... ... )

Function delete() ( ... ... )

... ... } ?>

Simple, right?. This controller will be saved as books_controller.php and placed in /app/controllers. It contains a list of functions that perform the actions for our example, as well as other functions for performing book-related operations (add a new book, delete a book, and so on).

The working environment provides us with many ready-made solutions and we only need to create a list of books. There is a base class in which the basic functionality of the controller is already defined, so you need to inherit the properties and functions of this class ( AppController is the heir Controller).

All you need to do in the action list is call the model to get the data and then select the view to present it to the user. Here's how it's done.

this->Book is our model, and part of the code:

$this->Book->findAllByCategory($category)

tells the model to return a list of books on the selected topic (we'll look at the model later).

Method set in line:

$this->set("books", $this->Book->findAllByCategory($category));

The controller passes data to the view. Variable books takes the data returned by the model and makes it available to the view.

The only thing left to do now is to display the view, but this function is done automatically in cakePHP if we use the default view. If we want to use a different view, then we must explicitly call the method render.

Model

The model is even simpler:

class Book extends AppModel(

Why is it empty? Because it inherits from a base class that provides the required functionality, and we need to use CakePHP's naming convention in order for the workbench to perform all other tasks automatically. For example, cakePHP knows from its name that this model is used in BooksController, and that it has access to a database table called books.

With this definition, we will have a model that can only read, delete, or store data in the database.

Save the code as book.php in folder /app/models.

View

All we need to do now is create a view (at least one) for the action list. The view will have HTML code and a few (as few as possible) lines of PHP code to loop through the array of books provided by the model.












Name Author Price

As you can see, the view does not create a full-fledged page, but only a fragment of HTML (a table in this case). Because CakePHP provides another way to define a page template, and the view is inserted into that template. The workbench also provides us with some helper objects to perform common tasks while creating parts of an HTML page (inserting forms, links, Ajax, or JavaScript).

Save the view as list.ctp(list is the action name and ctp means CakePHP template) in the folder /app/views/books(because it's a view for a controller action).

This is how all three components are executed using the CakePHP workbench!

Hi all! Today I am writing the first article in a series on creating your own MVC applications.

To begin with, it is worth noting that if someone does not know, what is MVC then read this article first: .

So what will we have at the end? We will have engine, created using MVC design pattern. This engine will be very simple, checks will be omitted somewhere, but this is all done so that you understand how to create applications on MVC, and then, having finalized our engine, you can use it for your projects. We will have the basic functionality:

  • authorization
  • small chat
  • adding articles
  • article editing
  • deleting articles
  • user management

It all starts with the folder structure. We will have it like this:

  • index.php
  • .htaccess
  • controllers
  • models
  • views
  • libs

I think everything is clear here. In folders controllers, models, views and libs will be kept controllers, models, kinds and other files. During the creation process, we will add the folders and files we need.

To create our engine will be used object-oriented approach. If you do not know him well, then you should also first read about this article on the site.

So, this is where I end the introductory article, and in the next one we will already begin to create engine on MVC. See you later!