21
NovIntroduction to ASP.NET MVC
ASP.NET MVC: An Overview
MVC stands for Model-View-Controller, a pattern that’s becoming increasingly popular with web development frameworks. ASP.NET MVC is an alternative and a complement to Web Forms, which means you won’t be dealing with pages and controls, postbacks or view states, or complicated ASP.NET event life cycles. In this MVC Tutorial, we will explore more about MVC which will include what is asp.net mvc, asp.net MVC features, ASP.NET MVC Web Application Advantages, benefits of asp.net MVC, and why asp.net MVC.
What is ASP.NET MVC?
Basically, MVC is a framework methodology that divides an application's implementation into three component roles: models, views, and controllers. Hence in Asp.net MVC, you need to play with controllers, actions, and views.
MVC Pattern
The Model-View-Controller (MVC) pattern is an adaptation of a pattern generated from the Smalltalk community in the 1970s by Trygve Reenskaug. It was popularized for use on the web with the advent of Ruby on Rails in 2003.
Model
Models in an MVC-based application are the components of the application that are responsible for maintaining the state. Often this state is persisted inside a database for example: we might have a Product class that is used to represent order data from the Products table inside SQL.
View
Views in an MVC-based application are the components responsible for displaying the application's user interface. Typically this UI is created off of the model data for example: we might create a Product "Edit" view that surfaces textboxes, dropdowns, and checkboxes based on the current state of a Product object.
Controller
Controllers in an MVC-based application are the components responsible for handling end-user interaction, manipulating the model, and ultimately choosing a view to render to display UI. In an MVC application, the view is only about displaying information - it is the controller that handles and responds to user input and interaction.
Why asp.net MVC?
- It has a separate layer structure the UI (Views) and logic (Controllers).
- Written with testability in mind. also it Simplifies TDD (Test-Driven Development)
- We can define our validation framework at the controller or model level.
- It has built-in support of jQuery, AJAX, and many other tools that comprise the MVC Ajax Toolkit
- It has Excellent support for Web API.
- asp.net MVC is open source.
- Also, It is easy to implement because it uses the same fundamental concepts as ASP.NET Web Forms but without web forms.
Features of the ASP.NET MVC
1. Attribute Routing
- The major difference between ASP.NET MVC and ASP.NET web forms is the way incoming requests are handled.
- MVC routing pattern maps URLs with action methods,
- To see where routes are configured, First go to the solution explorer of ASP.NET MVC application find the App_Start folder, and find the RouteConfig.cs file.
- It contains a method named RegisterRoutes.
- Inside this RegisterRoutes method are routes that are to be ignored,routes that are to be added can be defined. This is shown in the following code snippet:
public static void RegisterRoutes(RouteCollection routes){ routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); routes.MapRoute( name: "Default", url: "{controller}/{action}/{id}", defaults: new { controller = "Home", action = "Index", id = UrlParameter.Option } ); }
- Apart from defining routes in the RouteConfig.cs file, ASP.NET MVC 5 has introduced a new feature.
- This new feature is called attribute routing which allows developers to specify a route as an attribute of the action method.
- To enable attribute routing, we need to modify the RegisterRoutes method of the RouteConfig.cs file as follows:
public static void RegisterRoutes(RouteCollection routes) { routes.MapMvcAttributeRoutes(); }
2. Bootstrap
3. Identity Management
ASP.NET MVC Web Application Advantages
The ASP.NET MVC Framework is a new framework and has the following advantages over Web Forms approach (means over ASP.Net):
Separation of concern
In ASP.NET MVC the application is divided into Model, View, and Controller parts which make it easier to manage the application complexity.
TDD
The ASP.NET MVC framework brings better support to test-driven development.
Extensible and pluggable
ASP.NET MVC framework components were designed to be pluggable and extensible and therefore can be replaced or customized easier then Web Forms.
Full control over application behavior
The ASP.NET MVC framework doesn’t use View State or server-based forms like Web Forms. This gives the application developer more control over the application's behaviors and also reduces the bandwidth of requests to the server.
ASP.NET features are supported
The ASP.NET MVC framework is built on top of ASP.NET and can, therefore, use most of the features that ASP.NET includes, such as the provider's architecture, authentication and authorization scenarios, membership and roles, caching, session, and more.
URL routing mechanism
ASP.NET MVC framework supports a powerful URL routing mechanism that helps to build a more comprehensible and searchable URL in your application. This mechanism helps to the application to be more addressable in the eyes of search engines and clients and can help in search engine optimization.
Benefits of asp.net MVC
1. Manages large-size web apps:
In MVC, web application logic is easy to divide and organize into large-scale applications. The big advantage of using such code practices is that it helps to find specific portions of code quickly and allows us to add of new functionality with ease.
2. Easy Modifications:
MVC allows easy modification of the entire application. Adding or updating the new type of views is simplified in the MVC pattern. So, if there are any changes in a certain section of the application then it will never affect the entire architecture.
3. Fast Development :
As there is a dividing of the code among the three levels, One developer can work on a view section while another can work on any other section like the controller simultaneously. This allows for easy implementation of business logic and the fastest development of the project.
4. Low maintenance :
MVC gives the developer an outline of how to arrange their ideas into actual code and is also a great tool to help limit code duplication and allow easy maintenance of the application.
5. Supports TTD (test-driven development) :
MVC simplifies the testing process and It makes it easier to debug large-scale applications as multiple levels are structurally defined and properly written in the application.
6. Multiple Views :
In MVC, developing different view components for your model component is easily achievable. It empowers developers to develop different view components, thus limiting code duplication as it separates data and business logic.
7. SEO-Friendly:
MVC supports the development of SEO-friendly web applications, To generate more visits from a particular application, MVC provides an easy way to develop SEO-friendly RESTful URLs.
Conclusion
The ASP.NET MVC simplifies the complex parts of ASP.net Web Forms without any compromise of the power and flexibility of the ASP.NET platform. ASP.net MVC implements a Model-View-Controller UI pattern for web application development that lets you develop applications in a loosely coupled manner. MVC pattern separates the application into three parts- Model, View, and Controller. Also, Consider our ASP.NET MVC Course for a better understanding of all MVC concepts.