ASP.Net MVC in a flash for Experienced
Request is received by Routing table
which creates RouteData Object [RouteCollection] and passes it to
IRouteHandler.
IRouteHandler decides which handler to
be invoked to handle the request, here in this case it would be MVC
Handler.
MVC Handler looks at the url and passes
the controller name to IController Factory which checks the
controller presence and returns Controller instance to Handler. Now
the Handler invokes the execute method of the returned Controller
instance and passes the request to ActionSelector.
Action Selector has two ways to handle
the action
- ActionName selector
- ActionMethod selectors
Action selector resolves the Action and
request is passed to IActionInvoker. By default Action selector loads
all the methods of the class and based on URL it calls the action
method.
IActionInvoker calls the
IActionFilter’s which runs in below sequence
- IAuthentication filter
- IAuthorization filter
- IAction filter
- IException filter
- IResult filter
The IAction filter makes use of
IModelBinder which in turn takes help of ValueProvider to resolve the
action parameter value(s) and the action method gets executed and the
result is passed to IViewEngine.
If Result is View or PartialView then
IViewEngine loads the
- WebForm View Engine(.aspx)
- Razor View Engine (.cshtml)
MVC does not support method overloading
for Action methods, we can achieve this by using Action Name or by
using Attribute [HTTPPost/ HTTPGet]
IModelBinder does 3 tasks
- Resolves value
- Conversion
- Validates
It takes help of
Value Provider to resolve/ get actual value.
ValueProvider
|
ChildActionValueProvider
|
FormValueProvider
|
JSONValueProvider
|
RouteDataValueProvider
|
QueryStringValueProvider
|
HttpFileCollectionValueProvider
|JQueryFormValueProvider
We can create
Custom ValueProvider if required. Eg:- To read from Cookie we can
extend ValueProvider.
To post a form we have 3 ways in MVC.
- Using parameter with button name in action
- Use hidden field set value before post
- Use Action attribute of the button in HTML5 which would change URL in browser.
When we add layout page to a View
bootstrap gets included in the project automatically.
Partial Views are used to send partial
HTML & avoid layout settings.
There are three ways in which Partial
View can be included in a page.
- @RenderPage
- is faster because it writes to Response.
- Does not use lookup method
- Is used for static content
- @HTML.Partial - Used with strongly typed views.
- @HTML.RenderPartial - Supports both strongly typed & non-static. Best suited for Ajaxified pages.
@Section are to be used for local
style/ css or something page specific, these sections are declared in
layout and in Views we can use these sections to embed Style/ css/ js
files.
Browsers have limitation to download 3
files at a time.
The JSONResult action returns Microsoft
JSON format, this JSON cannot be used with JQuery directly. JQuery
understands NewtonSoft JSON, so this has to be converted to
NewtonSoft JSON.
Ajax Helpers
Four steps should be followed to use
Ajax helpers
- Install unobtrusive javascript
- Use it as Ajax.ActionLink
- Provider the Ajax options to method
- Include validate.js & unobtrusive.jquery.validation.js
To get the validation work
- Set config setting unobtrusive & client validation to true.
- Apply Data annotation attribute on properties.
- Only use strongly typed helpers or template helpers to generate UI.
- Refer jquery Validate & unobtrusive js files on page
- ModelState to be checked before operation.
Customising Routes
We customise routes only when URL
parameters must be altered.
- Change parameter name [id to something(PId)].
- Change parameter order
- Update no. of parameters
- Change URL value to controller name, a kind of mapping to avoid the end client know the controller name and action name.
We can add constraints to route which
would validate the value type using Regex.
Declarative Helpers
This feature helps us to avoid creating
partial view for displaying content which have less text. For eg:- If
we have to display DateTime on each page we can create cshtml and
keep it inside the App_Code folder which gets complied to a class and
at runtime is available to Views.
Cshtml would look like
@helper
HelloWorld(string
message)
{
<h1>Welcome
@message</h1>
<h2>@DateTime.ToString()
</h2>
}
In the Views we include Declarative
helpers as @<FileName>. <METHOD_NAME>
No comments:
Post a Comment