It explains about what is MVC Pattern and what is ASP.NET MVC Framework how it works. Also explains ASP.NET MVC page life cycle and ASP.NET MVC features version wise.
Samples provided steps wise to help beginners to understand easily and become proficient in ASP.NET MVC.
As we all know many design patterns and using them in implementing business components and services in our applications but still we will be facing issues/challenges with our applications. Also day by day business needs and priorities will be changing. If we closely observe a number of issues, defects and challenges we are facing is in UI and presentation layers. Though some defects are related to business logic and business rules we may need to fix them in UI and presentation layers as we might tightly integrated business logic in UI and presentation tiers. The reason behind this is we haven’t focused on implementing right design pattern in our applications. Let us go through step by step and understand how to implement and use presentation pattern in our applications.
In Presentation layer,
What are the presentation layer patterns available?
MVC (Model View Controller)
MVP (Model View Presenter) or (Model Passive View, Supervisor Controller)
MVVM (Model View ViewModel)
Model and View represents same in all the above 3 patterns?
Yes
Controller, Presenter, and ViewModel purpose is same in all the above 3 patterns?
Yes
Communication and flow of Model, View with Controller, Presenter, and ViewModel is same?
No, that is the reason these 3 patterns exists.
Are these patterns replacement of PL (Presentation Layer), BLL (Business Logic Layer) and DAL (Data Access Layer)
No, these patterns are for separating the UI and UI Logic from Presentation Logic and enables the loose coupling.
MVP
MVC
Note: Here I am not focusing on MVC VM (MVC ViewModel from MVC3) and ASP.NET MVVM with Dependency Injection.
MVVM
ASP.NET Web Forms
ViewState
& No postback supportASP.NET MVC
Model, Views & Controller
Templated Helpers helps us to automatically associate HTML elements for edit and display with data types.
E.g when data of type System.DateTime
is displayed in a view, a datepicker UI element can be automatically rendered.
This is similar to how field templates work in ASP.NET Dynamic Data.
Using areas We can organize a large project into multiple smaller sections in order to manage the complexity of a large Web application.
Each section (“area”) typically represents a separate section of a large Web site and is used to group related sets of controllers and views.
E.g.
Areas Admin Controllers Models Views Iniala Claims Controllers Models Views
ASP.NET MVC2 allows controllers to process requests asynchronously.
This can lead to performance gains by allowing servers which frequently call blocking operations (like network requests) to call non-blocking counterparts instead.
DefaultValueAttribute
in Action-Method Parameters:The System.ComponentModel.DefaultValueAttribute
class allows a default value to be supplied for the argument parameter to an action method.
For example, assume that the following default route is defined:
{controller}/{action}/{id}
Also assume that the following controller and action method is defined:
public class ArticleController { public ActionResult View(int id, [DefaultValue(1)]int page) { } }
Any of the following request URLs will invoke the View action method that is defined in the preceding example.
There are two new overloads of the Html.Hidden
helper that encode binary values as base-64-encoded strings:
public static string Hidden(this HtmlHelper htmlHelper, string name, Binary value); public static string Hidden(this HtmlHelper htmlHelper, string name, byte[] value);
DataAnnotations
Attributes:Using the RangeAttribute
, RequiredAttribute
, StringLengthAttribute
, and RegexAttribute
validation attributes (defined in the System.ComponentModel.DataAnnotations
namespace) when we bind to a model in order to provide input validation.
using System.ComponentModel.DataAnnotations; namespace MvcTmpHlprs { [MetadataType(typeof(ProductMD))] public partial class Product { public class ProductMD { public object SellStartDate { get; set; } [UIHint("rbDate")] public object SellEndDate { get; set; } [DataType(DataType.Date)] public object DiscontinuedDate { get; set; } [ScaffoldColumn(false)] public object ModifiedDate { get; set; } [ScaffoldColumn(false)] public object rowguid { get; set; } [ScaffoldColumn(false)] public object ThumbnailPhotoFileName { get; set; } } } }
The model-validation provider class represents an abstraction that provides validation logic for the model.
ASP.NET MVC includes a default provider based on validation attributes that are included in the System.ComponentModel.DataAnnotations
namespace.
The model-validator provider class exposes validation metadata to the browser in the form of JSON-serialized data that can be consumed by a client-side validation library.
ASP.NET MVC 2 includes a client validation library and adapter that supports the DataAnnotations
namespace validation attributes noted earlier.
RequireHttpsAttribute
Action Filter:ASP.NET MVC 2 includes a new RequireHttpsAttribute
class that can be applied to action methods and controllers.
By default, the filter redirects a non-SSL (HTTP) request to the SSL-enabled (HTTPS) equivalent.
When we build a Website by using the REST architectural style, HTTP verbs are used to determine which action to perform for a resource.
REST requires that applications support the full range of common HTTP verbs, including GET
, PUT
, POST
, and DELETE
.
ASP.NET MVC 2 includes new attributes that we can apply to action methods and that feature compact syntax.
These attributes enable ASP.NET MVC to select an action method based on the HTTP verb.
For example, a POST
request will call the first action method and a PUT
request will call the second action method.
[HttpPost] public ActionResult Edit(int id) [HttpPut] public ActionResult Edit(int id, Tag tag)
In earlier versions of ASP.NET MVC, these action methods required more verbose syntax, as shown in the following Example:
[AcceptVerbs(HttpVerbs.Post)] public ActionResult Edit(int id) [AcceptVerbs(HttpVerbs.Put)] public ActionResult Edit(int id, Tag tag)
Because browsers support only the GET
and POST
HTTP verbs, it is not possible to post to an action that requires a different verb. Thus it is not possible to natively support all RESTful
requests.
However, to support RESTful
requests during POST
operations, ASP.NET MVC 2 introduces a new HttpMethodOverride
HTML helper method.
This method renders a hidden input element that causes the form to effectively emulate any HTTP method.
For example, by using the HttpMethodOverride
HTML helper method, we can have a form submission appear be a PUT
or DELETE
request.
The behavior of HttpMethodOverride
affects the following attributes:
HttpPostAttribute
HttpPutAttribute
HttpGetAttribute
HttpDeleteAttribute
AcceptVerbsAttribute
HiddenInputAttribute
Class for Templated Helpers:We can apply the new HiddenInputAttribute
attribute to a model property to indicate whether a hidden input element should be rendered when displaying the model in an editor template (The attribute sets an implicit UIHint
value of HiddenInput
).
The attribute’s DisplayValue
property lets we specify whether the value is displayed in editor and display modes.
When DisplayValue
is set to false, nothing is displayed, not even the HTML markup that normally surrounds a field.
The default value for DisplayValue
is true.
We might use HiddenInputAttribute
attribute in the following scenarios:
In that case, the value and surrounding HTML markup (such as the label and value) are not displayed.
E.g:
public class ProductViewModel { [HiddenInput] // equivalent to [HiddenInput(DisplayValue=true)] public int Id { get; set; } public string Name { get; set; } [HiddenInput(DisplayValue=false)] public byte[] TimeStamp { get; set; } }
Html.ValidationSummary
Helper Method Can Display Model-Level Errors:Instead of always displaying all validation errors, the Html.ValidationSummary
helper method has a new option to display only model-level errors.
This enables model-level errors to be displayed in the validation summary and field-specific errors to be displayed next to each field.
A new property is available to T4 files from the ASP.NET MVC T4 host that specifies the version of the .NET Framework that is used by the application.
This enables T4 templates to generate code and markup that is specific to a version of the .NET Framework.
In Visual Studio 2008, the value is always .NET 3.5. In Visual Studio 2010, the value is either .NET 3.5 or .NET4.
Added a protected virtual CreateActionInvoker
method in the Controller class.
This method is invoked by the ActionInvoker
property of Controller and allows for lazy instantiation of the invoker if no invoker is already set.