MVC


1.     What is MVC?
2.     Why we use ASP.NET MVC?
3.     ASP.NET MVC Architecture.
4.     ASP.NET MVC life Cycle?
5.     Model, View and Controller.
6.     Types of Routing.
7.     Action Methods.
8.     HTML Helpers.
9.     Types of Views.
10.   ViewBag, ViewData and TempData.
11.   What is Area in MVC?
12.   Advantages and Disadvantage.


What is MVC?
Ø  The Model-View-Controller(MVC) is an architectural pattern or a  software architectural pattern for implementing user interfaces on computers.
Ø  It’s just a three layer architecture where M stands for MODEL, V stands for VIEW, and the most important part in this architecture is CONTROLLER, like a Hero of any film.
Ø  Every layer in MVC is assigned with a unique responsibility.
Ø  Popular programming languages like Java, C#, Ruby, PHP and others have popular MVC frameworks that are currently being used in web application development

Fetures of ASP.NET MVC ?
1)      The ASP.NET MVC framework provides an alternative to the ASP.NET Web Forms pattern for creating Web applications.
2)      The ASP.NET MVC framework is a lightweight, highly testable presentation framework that is integrated with existing ASP.NET features.
3)      The MVC framework is defined in the System.Web.Mvc assembly.
4)      The MVC pattern helps you create applications that separate the different aspects of the application (input logic, business logic, and UI logic)
Why ASP.NET MVC?


2-      ASP.NET MVC Architecture
Visual Studio creates the following folder structure for MVC application by default.
App_Data:
App_Data folder can contain application data files like LocalDB, .mdf files, xml files and other data related files. IIS will never serve files from App_Data folder.
App_Start:
App_Start folder can contain class files which will be executed when the application starts. Typically, these would be config files like AuthConfig.cs, BundleConfig.cs, FilterConfig.cs, RouteConfig.cs etc. MVC 5 includes BundleConfig.cs, FilterConfig.cs and RouteConfig.cs by default. We will see significance of these files later.
Content:
Content folder contains static files like css files, images and icons files. MVC 5 application includes bootstrap.css, bootstrap.min.css and Site.css by default.

4) Life Cycle of MVC :

Routing : Asp.net Routing is the first step in MVC request cycle. Basically it is a pattern matching system that matches the request’s URL against the registered URL patterns in the Route Table. When a matching pattern found in the Route Table, the Routing engine forwards the request to the corresponding IRouteHandler for that request. The default one calls the MvcHandler. The routing engine returns a 404 HTTP status code against that request if the patterns is not found in the Route Table.
MvcHandler : The MvcHandler is responsible for
initiating the real processing inside ASP.NET MVC.
MVC handler implements IHttpHandler interface and
further process the request by using ProcessRequest
method.
Controller : MvcHandler uses the IControllerFactory
instance and tries to get a IController instance. If
successful, the Execute method is called. The
IControllerFactory could be the default controller factory or
a custom factory initialized at the Application_Start event.
4. Authentication and Authorization : Authentication is the process of verifying the identity of a user by obtaining
some sort of credentials and using those credentials to verify the user's  identity. If the credentials are valid, the authorization process starts. Authorization is the process of allowing an authenticated users to access the resources by checking whether the user has access rights to the  system. Authorization helps you to control access rights by granting or denying specific permissions to an authenticated user.
5. Model Binding : Model Binding allows you to map and bind the HTTP request data with a model.
6. Action Method Invocation: Once the controller has been instantiated, Controller's ActionInvoker determines   which specific action to invoke on the controller. Action to be execute is chosen based on attributes ActionNameSelectorAttribute(by default method which have the same name as the action is chosen) and ActionMethodSelectorAttribute(If more than one method found, the correct one is chosen with the help of this attribute).
7. View :  Action method may returns a text string,a binary file or a Json formatted data. The most important Action Result is the ViewResult, which renders and returns an HTML page to the browser by using the current view engine.


Model in ASP.NET MVC:
1)      The model is the part of the application that is responsible for the business logic.
2)      MVC model is basically a C# or VB.NET class.
3)      A model is accessible by both controller and view.
4)      A model can be used to pass data from Controller to view.
5)      A view can use model to display data in page.

2- View in ASP.NET MVC:
1)      View is a user interface
2)      View displays data from the model to the user and also enables them to modify the data.
3)      ASP.NET MVC views are stored in Views folder.
4)      Different action methods of a single controller class can render different views
5)      No server tag in view.
6)      A request to view can be made only from a controller’s action method.

3- Controller in ASP.NET MVC:
1)      The Controller in MVC architecture handles any incoming URL request.
2)      Controller is a class, derived from the base class System.Web.Mvc.Controller.
3)      Controller class contains public methods called Action methods.
4)      Controller and its action method handles incoming browser requests, retrieves necessary model data and returns appropriate responses.
5)      In ASP.NET MVC, every controller class name must end with a word "Controller". For example, controller for home page must be HomeController and controller for student must be StudentController. Also, every controller class must be located in Controller folder of MVC folder structure.
Types of Routing
----------------------------
Default Routing :
Routing plays important role in MVC framework. Routing maps URL to physical file or class (controller
class in MVC).
2.    Route contains URL pattern and handler information. URL pattern starts after domain name.
3.    Routes can be configured in RouteConfig class. Multiple custom routes can also be configured.
Route constraints applies restrictions on the value of parameters.
The route with the pattern {resource}.axd/{*pathInfo} is included to prevent requests for the Web resource files such as WebResource.axd or ScriptResource.axd from being passed to a controller.
6.    Route must be registered in Application_Start event in Global.ascx.cs file.

Attribute Routing : In order to expose a controller action method by a certain url, we can use attribute routing. specifying the route with the help of attribute on action methods. To achieve this we need to modify our App_Start/RouteConfig.cs file a bit.

No comments:

Post a Comment