Thursday, August 11, 2016

Miscellanous Reading



 There are following advantages of ASP.NET MVC over Web Forms (ASP.Net):
Separation of concern - MVC design pattern divides the ASP.NET MVC application into three main aspects
Model, View and Controller which make it easier to manage the application complexity.
TDD - The MVC framework brings better support to test-driven development.
Extensible and pluggable - 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 behaviour - MVC framework doesn’t use View State or server based forms
like Web Forms. This gives the application developer more control over the behaviors of the application
and also reduces the bandwidth of requests to the server.
ASP.NET features are supported - MVC framework is built on top of ASP.NET and therefore can use most
of the features that ASP.NET include such as the providers architecture, authentication and authorization
scenarios, membership and roles, caching, session and more.
URL routing mechanism - MVC framework supports a powerful URL routing mechanism that helps to build a more comprehensible and searchable URLs in your application. This mechanism helps to the application to be more addressable from the eyes of search engines and clients and can help in search engine optimization.







































Page Life Cycle of ASP.NET MVC?
The detail pipeline of ASP.NET MVC is given below:
1. Routing - Routing is the first step in ASP.NET MVC pipeline. Typically, it is a pattern matching system that
matches the incoming request to the registered URL patterns in the Route Table.
The UrlRoutingModule(System.Web.Routing.UrlRoutingModule) is a class which matches an incoming HTTP request to a registered route pattern in the RouteTable(System.Web.Routing.RouteTable).


2. Controller Initialization - The MvcHandler initiates the real processing inside ASP.NET MVC pipeline by using ProcessRequest method. This method uses the IControllerFactory instance (default is
System.Web.Mvc.DefaultControllerFactory) to create corresponding controller.


3. Action Execution – Action execution occurs in the following steps:
When the controller is initialized, the controller calls its own InvokeAction() method by passing the details of the chosen action method. This is handled by the IActionInvoker.
After chosen of appropriate action method, model binders(default is System.Web.Mvc.DefaultModelBinder) retrieves the data from incoming HTTP request and do the data
type conversion, data validation such as required or date format etc. and also take care of input values
mapping to that action method parameters.
Authentication Filter was introduced with ASP.NET MVC5 that run prior to authorization filter. It is used
to authenticate a user. Authentication filter process user credentials in the request and provide a
corresponding principal. Prior to ASP.NET MVC5, you use authorization filter for authentication and
authorization to a user.
By default, Authenticate attribute is used to perform Authentication. You can easily create your own
custom authentication filter by implementing IAuthenticationFilter.
Authorization filter allow you to perform authorization process for an authenticated user. For example,
Role based authorization for users to access resources.
By default, Authorize attribute is used to perform authorization. You can also make your own custom
authorization filter by implementing IAuthorizationFilter.
Action filters are executed before (OnActionExecuting) and after (OnActionExecuted) an action is
executed. IActionFilter interface provides you two methods OnActionExecuting and OnActionExecuted
methods which will be executed before and after an action gets executed respectively. You can also make
your own custom ActionFilters filter by implementing IActionFilter. For more about filters refer this article
Understanding ASP.NET MVC Filters and Attributes
When action is executed, it process the user inputs with the help of model (Business Model or Data Model) and prepare Action Result.
 


 4. Result Execution - Result execution occurs in the following steps:
Result filters are executed before (OnResultnExecuting) and after (OnResultExecuted) the ActionResult is executed. IResultFilter interface provides you two methods OnResultExecuting and OnResultExecuted
methods which will be executed before and after an ActionResult gets executed respectively. You can also
make your own custom ResultFilters filter by implementing IResultFilter.
Action Result is prepared by performing operations on user inputs with the help of BAL or DAL. The Action Result type can be ViewResult, PartialViewResult, RedirectToRouteResult, RedirectResult, ContentResult, JsonResult, FileResult and EmptyResult.
Various Result type provided by the ASP.NET MVC can be categorized into two category- ViewResult type and NonViewResult type. The Result type which renders and returns an HTML page to the browser, falls into ViewResult category and other result type which returns only data either in text format, binary format or a JSON format, falls into NonViewResult category.



4.1 View Initialization and Rendering - View Initialization and Rendering execution occurs in the following steps:
ViewResult type i.e. view and partial view are represented by IView (System.Web.Mvc.IView) interface
and rendered by the appropriate View Engine.

This process is handled by IViewEngine (System.Web.Mvc.IViewEngine) interface of the view engine. By

default ASP.NET MVC provides WebForm and Razor view engines. You can also create your custom engine
by using IViewEngine interface and can registered your custom view engine in to your Asp.Net MVC
application as shown below:
Html Helpers are used to write input fields, create links based on the routes, AJAX-enabled forms, links
and much more. Html Helpers are extension methods of the HtmlHelper class and can be further extended very easily. In more complex scenario, it might render a form with client side validation with the help of JavaScript or jQuery.




Asp.Net Web Forms
Asp.Net MVC
Asp.Net Web Form follows a traditional event
driven development model.
Asp.Net MVC is a lightweight and follow MVC (Model,  View, and Controller) pattern based development model.
Asp.Net Web Form has server controls.
Asp.Net MVC has html helpers.
Asp.Net Web Form has state management (like as view state, session) techniques.
Asp.Net MVC has no automatic state management techniques.
Asp.Net Web Form has file-based URLs means file name exist in the URLs must have its physical existence.
Asp.Net MVC has route-based URLs means URLs are divided into controllers and actions and moreover it is based on controller not on physical file.
Asp.Net Web Form follows WebForm Syntax                                                                
Asp.Net MVC follow customizable syntax (Razor as default)
In Asp.Net Web Form, Web Forms (ASPX) i.e. views are tightly coupled to Code behind (ASPX.CS) i.e. logic.
In Asp.Net MVC, Views and logic are kept separately.
Asp.Net Web Form has Master Pages for consistent look and feels.
Asp.Net MVC has Layouts for consistent look and feels.
Asp.Net Web Form has User Controls for code reusability.
Asp.Net MVC has Partial Views for code re-usability.
Asp.Net Web Form has built-in data controls and best for rapid development with powerful data access.
Asp.Net MVC is lightweight, provide full control over mark-up and support many features that allow fast & agile development. Hence it is best for developing interactive web application with latest web standards.
Asp.Net Web Form is not Open Source.
Asp.Net Web MVC is an Open Source.








How to make Custom View Engine?
Ans. ASP.NET MVC is an open source and highly extensible framework. By default Razor engine supports C#
and VB. But you can customize the Razor View engine for C# and VB language. If your MVC application is using
only C# language, there is no need to looks up the views having extensions .vbhtml and same for VB language.
public class CSharpRazorViewEngine : RazorViewEngine
{
public CSharpRazorViewEngine()
{
AreaViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaMasterLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
AreaPartialViewLocationFormats = new[]
{
"~/Areas/{2}/Views/{1}/{0}.cshtml",
"~/Areas/{2}/Views/Shared/{0}.cshtml"
};
ViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
MasterLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
PartialViewLocationFormats = new[]
{
"~/Views/{1}/{0}.cshtml",
"~/Views/Shared/{0}.cshtml"
};
}
}


Registering Custom C# Razor View Engine
protected void Application_Start()
{
//Remove All View Engine including Webform and Razor
ViewEngines.Engines.Clear();
//Register C# Razor View Engine
ViewEngines.Engines.Add(new CSharpRazorViewEngine());
//other code is removed for clarity
}



  What is Validation Summary?
Ans. The ValidationSummary helper displays an unordered list of all validation errors in
the ModelState dictionary. It accepts a boolean value (i.e. true or false) and based on boolean value it display the errors. When boolean parameter value is true, it shows only model-level errors and excludes model property-level errors (i.e any errors that are associated with a specific model property). When Boolean value is false, it shows both model-level and property-level errors.


Suppose, you have the following lines of code somewhere in the controller action rendering a view:
ModelState.AddModelError("", "This is Model-level error!");
ModelState.AddModelError("Name", "This Model property-level error!");
 

In the first error there is no key to associate this error with a specific property. In the second error there is a key named as “Name” to associate this error for model property 'Name'.

@Html.ValidationSummary(true) @*//shows model-level errors*@
@Html.ValidationSummary(false) @*//shows model-level and property-level errors*@
 

Hence, when boolean type parameter value is true then ValidationSummary will display only model-level errors and exclude property-level errors. It will display Model-level and property-level errors, when boolean type parameter value is false. 

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More