Thursday 11 December 2014

MVC Intro

ASP.NET MVC

  • ASP.NET MVC is a free and fully supported framework for building web applications 
     
  • That use the model-view-controller pattern.

  • This means you can use in ASP.NET MVC applications the same APIs for security, state 
    management, membership, caching, and so on.,

  • Every ASP.NET MVC application has three core parts: a model, views, and controllers. 
     
  • In short, the model consists of all the classes that handle data and business logic. Data processing using model classes is initiated by the controllers that are in charge of user requests. Once the data processing is complete the controller creates a response to the user by sending the results to a View who then produces HTML to be rendered in the browser.







  • The model in the MVC pattern represents the parts of the application that implement the data domain logic.

  • Views are the visible elements in the application. They are the components that typically show users data from the model. A view page typically receives a view model object from the controller (the view doesn’t care how this data was obtained—that’s the controller’s responsibility!). The view page contains HTML (and possibly some UI-related code) to determine how to render the model data back to the browser.
  • The controllers are classes that collect the user requests, work with the model, and ultimately select a view to render the appropriate UI.

When to Use ASP.NET MVC

A high level of control over the generated HTML
Easier unit testing
Separation of concerns (model, views, and controllers) which makes it easy to maintain.



ASP.NET MVC Benefits

Compared to Web Forms:
  • The MVC pattern itself makes it easier to manage complexity by clearly separating the functionality of the application into three core parts, the model, the view, and the controller.
  • ASP.NET MVC web applications do not use view state or server-based forms. This makes the MVC framework ideal for developers who want full control over the behavior of an application. View state can become very large, which is a problem for devices like smart-phones running over slow networks (transmitting all that information can be very slow). In a Web Forms page, you could only have one <form> per page. This is quite a major restriction. In MVC, there is no such restriction—that is, you can have as many<form> elements as you like.
  • ASP.NET MVC provides better support for test-driven development (TDD).

  • ASP.NET MVC works well for web applications that are supported by large teams of developers and for web designers who need a high degree of control over the HTML.

ASP.NET MVC Request Processing

One of the most important concepts to understand about MVC applications is that no relationship exists between a page request and a physical file inside the web server. In a traditional Web Forms and Web Pages application, every page request is translated into a call to a physical file in the web server. For example, if your request is something like Http://systemssopt.blogspot.aspx, the web server interprets the request by looking at the root of the website for a file named blogspot.aspx.It then processes the file and returns the generated HTML.










In the case of an MVC application, when you make a request(e.g.,Http://systemssopt.blogspot/product/list), a component called routing engine matches the request to a specific route. A route defines requests using a pattern string and establishes the controller and method in the controller class that should process the request. Once the route is identified, the routing engine creates a request handler that in turn will create the controller object that will process the request (in our example, the controller is “product”). The controller then invokes the method in the controller class that will process the request (in the example is named “list”). These methods in controller classes that process requests are called action methods. When the processing of the request ends, the action method produces a result to send back to the user. Normally the result is some HTML (rendered by a View) the user will see in the browser.

ASP.NET MVC 4 Features

ASP.NET MVC 4 is built on top of the many features of its previous versions and include new features like:

  • ASP.NET Web API, a new framework for building HTTP and RESTful services.

  • A new HTML5-based default template in Visual Studio and a new Mobile Application project template.

  • Automatic selection of rendered views with Display Modes. This is particularly useful when building applications that will run not only on desktop browsers but on mobile browsers as well. It will let the application determine the best view to render based on the browser making the request.

  • jQuery Mobile and mobile features
    .
  • Task support for asynchronous controllers.

  • Microsoft Windows Azure SDK support for deploying ASP.NET MVC 4 applications to Windows Azure.

  • Bundling and minification for CSS and JavaScript files to help reduce the amount of HTTP requests as well as the size of those requests.

  • Facebook, OpenID, and OAuth authentication.

















































 








































 Choose a View Engine

ASP.NET MVC is highly configurable and allows you to select a view engine in the New Project dialog. A view engine is simply a templating language that will ultimately generate HTML in your application once the view has been processed. You need to select one view engine, but you are not limited to the out-of-the-box options, which are ASPX (or the Web Forms view engine) and Razor













































 











ASP.NET MVC 4 Internet Application Directory Structure


App_Data

You can place in this folder the files for which you require read/write permissions,

such as LocalDB database files.

App_Start

This folder contains the configuration files for the different technologies you use in the application, such as authentication, bundling, filtering, routing, and Web API.

Content

This folder is designed for CSS files and other assets in your website design.

Controllers

This folder is where you create controller classes. These classes are in charge of handling user requests. The methods in the classes are called action methods because they return some kind of action result. The action result can be HTML, a file, an object, or even an exception. Action methods and controllers are mapped to request URLs, which is how controllers know how to handle the requests.

Filters

Action filters are custom attributes that provide a declarative means to add a specific

behavior to controller action methods. They are used to simplify the implementation of logic in action methods that otherwise would have to be added manually (and repetitively) in every action method. They facilitate the centralization of logic that will then be reused by any action method that needs it.

Images

This folder is a place to store the images you will be using in your website.

Models

This is the folder where you create the data and business logic classes.

Scripts

You add JavaScript files to this folder.

Views

This folder is for the files containing the UI logic. The files are normally just called views and use a syntax defined by the view engine you selected when you created the project. Views are used to render HTML to the client browser.

Properties

Double clicking on the Properties node will open the project’s properties window where you can modify options such as the Dot NET version used by the project, build and publishing options and many more. Expanding the node gives you access to the AssemblyInfo.cs file that stores metadata associated with the project’s assembly.

References

The References node contains all assemblies used in the application.

Configuration files

The Web.Config and App.Config files are files in XML format that define configuration

options for ASP.NET applications.

Global.asax

Is a file where ASP.NET applications declare and handle application and session-level events.

Packages.Config

Is a file in XML format that stores the information of NuGet packages~(NuGet is the package manager for the Microsoft development platform including Dot NET.) installed in the application.



©Prasathvel

0 comments:

Post a Comment