An Introduction to ASP.NET MVC 1.0
Shared by: linxiaoqin
-
Stats
- views:
- 0
- posted:
- 3/10/2012
- language:
- pages:
- 36
Document Sample


Keyvan Nayyeri
http://nayyeri.net
Classic ASP
The .NET Framework
ASP.NET 1.0
ASP.NET 1.1
ASP.NET 2.0
ASP.NET AJAX
ASP.NET 3.5
ASP.NET 4.0
Desktop-like development with controls and
events
Classic model of development with wizards
and controls
Ease of state management
Ease of tooling
The lack of best practices for development
The lack of control over project elements
The lack of control over rendered HTML code
The lack of separation of concerns (and
testability)
Many repetitions of code in a project
Heavy pages to accomplish state
management (ViewState)
Lower level of flexibility and customizability
Resolving the weaknesses with ASP.NET
WebForms
Responding to new requirements of today’s
Software/Web Development
Having the market share regarding the
success of Ruby on Rails and other
technologies
ASP.NET WebForms as the new and correct
name for original ASP.NET technology
ASP.NET Dynamic Data for Data-Driven web
development
ASP.NET MVC for web development with
MVC pattern
Splitting the implementation of an application
into three components
Providing a high level of separation of
concerns that improves the testability and
maintainability
Suitable for applications that deal with data
storage and user interactions
Model
Business Logic
Data Interaction
View
User Interaction
Controller
The bridge between the model and the view
Implements the MVC pattern for ASP.NET
Replaces the fundamental classes in ASP.NET
with appropriate implementation
Applies the core ASP.NET API with relevant
implementation for ASP.NET MVC
development
Provides a high level of control,
customizability, flexibility, and extensibility
High level of separation of concerns
A default project structure
Naming convention over configuration
Open to change and expansion
Ready to integrate with third party
components and libraries
Basic but powerful core to be used by
developers
WebForms applications are faster to develop
MVC applications are easier to maintain
MVC applications have higher quality
MVC applications are easier to test
MVC applications are easier to expand and
customize
MVC applications take more effort for state
management
MVC applications have user interfaces that
are compliant to web-standards
WebForms is neither dead nor classic
Testability is a benefit of good separation of
concerns and ASP.NET MVC is not about unit
testing
WebForms is still a great technology with
some advantages over MVC
MVC is not supposed to be as classic as
WebForms and there shouldn’t be much
tooling around it
No event-based model anymore
Request filtering based on HTTP verbs in
Action Methods replaces the event model in
ASP.NET WebForms
A combination of user interface elements and
programming code to load and display data in
views
Similar to classic ASP but no business logic is
embedded in views
Usage of interfaces and abstract base classes
for abstraction is strongly recommended
Controllers in Controllers folder
Model and data interaction files
in Models folder
Views, Master Pages, and User
Controls in Views folder
JavaScript files and jQuery
library in Scripts folder
By default ASP.NET MVC
doesn’t work property without
the correct structure of files and
folders
Model
Controller
Action Method
Action Filter
View
HtmlHelper
Routing
Responsible for data interaction and business
logic implementation
The ambiguous part of ASP.NET MVC
M is the only part of MVC that is not
implemented in ASP.NET MVC
There is only a Models folder in project
template
You can use your favorite technology: LINQ to
SQL, Entity Framework, NHibernate, …
Acts like a bridge between the model and the
view
It’s a wrong belief to put business logic in
controller
Settings can be applied to this component
It can be considered as one of the simplest
parts of an ASP.NET MVC application
Located in Controllers folder
A class derived from Controller base class
The class name should consist of the
controller name + “Controller”
The main job is done by public methods inside
the controller (Action Methods)
Unit testing primarily deals with testing the
controller and its Action Methods.
A simple controller class
using System.Web.Mvc;
namespace SampleMvcApplication.Controllers
{
public class HomeController : Controller
{
public ActionResult Index()
{
ViewData["Message"] = "Welcome to ASP.NET MVC!";
return View();
}
public ActionResult About()
{
return View();
}
}
}
A public method in a controller
Gets arbitrary number of parameters from
URL or request
Proceeds with the request and passes the
appropriate data items from the model to the
view or vice versa
Returns an instance of ActionResult or other
derivations for specific purposes
There are various derivations of ActionResult
in the framework or you can write your own
An attribute that can be applied to the action
methods
Used to apply the settings or customize the
behavior
There are many Action Filters in the
framework
You can write your own Action Filters
Some common examples of Action Filters are
for Authorization, Caching, and HTTP verb
filtering
A simple Action Filter
[Authorize]
public ActionResult ChangePassword()
{
ViewData["PasswordLength"] = MembershipService.MinPasswordLength;
return View();
}
Responsible for user interaction
Views have similar responsibilities as pages in
ASP.NET WebForms
Master pages and user controls have the same
responsibilities as ASP.NET WebForms
Designers and developers should build user
interfaces with basic HTML elements with
creativity
Basic HTML elements are provided via
HtmlHelpers
A simple View
<%@ Page Language="C#" MasterPageFile="~/Views/Shared/Site.Master" Inherits="System.Web.Mvc.ViewPage" %>
<asp:Content ID="indexTitle" ContentPlaceHolderID="TitleContent" runat="server">
Home Page
</asp:Content>
<asp:Content ID="indexContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>
<%= Html.Encode(ViewData["Message"]) %></h2>
<p>
To learn more about ASP.NET MVC visit <a href="http://asp.net/mvc" title="ASP.NET MVC Website">
http://asp.net/mvc</a>.
</p>
</asp:Content>
A set of helper methods attached to the Html
property of the ViewPage
Provide basic user interface elements such as
TextBox or Button
One of the good extensibility points to
develop your own helper method
There are already some community
extensions such as HtmlHelper for Gravatar or
ReCaptcha
Enables RESTful access to resources in
ASP.NET
Not a specific part of MVC or ASP.NET MVC
Responsible to map requests based on their
pattern to appropriate controller and action
methods
Can be modified and expanded in several
ways
Ends with better URLs for ASP.NET
applications
A simple routing definition
using System.Web.Mvc;
using System.Web.Routing;
namespace SampleMvcApplication
{
public class MvcApplication : System.Web.HttpApplication
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = "" } // Parameter defaults
);
}
protected void Application_Start()
{
RegisterRoutes(RouteTable.Routes);
}
}
}
ASP.NET MVC uses the same API as ASP.NET
WebForms with minor changes in usage
Configurative approach is still in place but
declarative usage with Action Filters is
preferred
Membership and Role providers can be used
The default project template has a rich
implementation that can be used easily
Form authentication can be implemented
with the same principles as ASP.NET
WebForms
AJAX is very different from ASP.NET
WebForms
Basic but key tools are provided as AjaxHelper
methods: ActionLink and BeginForm
Complex AJAX scenarios can be built based on
these tools
Third party libraries can be used easily
jQuery is distributed as a part of ASP.NET
MVC project template with Visual Studio
Intellisense
MVC development is tied to some of the
modern development approaches
Dependency Injection can be achieved with
abstraction in your code
Dependency Injection helps ameliorating
Test-Driven Development and maintenance
Unit Testing helps finding the possible holes in
your code
Mainly consists of comparing the ViewData
items in an Action Method with your
expectations
You have to follow abstraction in your code to
make testing easier
You can use mock frameworks to speed up
the unit testing process
Other part of the application (i.e. routing) can
be tested as well
ASP.NET MVC is very extensible
Almost everything in ASP.NET MVC can be
replaced or expanded
Some community projects focus on
alternative implementations for ASP.NET
MVC components such as ViewEngine or
Controller Factory
MvcContrib is a notable project available at
http://www.codeplex.com/MVCContrib
Many times migration is not necessary
Migration can consist of easy and difficult
steps
The process varies significantly by original
implementation and features that are used
Migration can be done smoothly with the
possibility to run ASP.NET WebForms and
ASP.NET MVC in the same project side-by-
side
You may need to find alternatives for some
parts such as server controls
Getting Started with ASP.NET MVC 1.0
DZone Refcard
By Simone Chiaretta and Keyvan Nayyeri
A compact and quick starter guide to ASP.NET
MVC 1.0
Will be available as free PDF and printed card
with high quality
Check out http://refcardz.dzone.com for
updates
Beginning ASP.NET MVC 1.0
By Simone Chiaretta and Keyvan Nayyeri
Published by Wrox (August 2009)
Dedicated to all Iranians
around the world
Covers all basic concepts
and principles
Discusses all the major
topics about ASP.NET MVC
with a pragmatic approach
Get documents about "