Wrox Press What's New in ASP.NET MVC 2 (2010).pdf
(
1722 KB
)
Pobierz
721497277 UNPDF
Simone Chiaretta
What’s New
in ASP.NET
MVC 2?
Wiley Publishing, Inc.
Updates, source code, and Wrox technical support at
www.wrox.com
Contents
Introducing ASP.NET MVC 2
1
Runs on Visual Studio 2008 and Visual Studio 2010
2
Templated Helpers and Model Metadata
2
HiddenInputAttribute
5
How to Write Custom Templates
7
Validation
8
Client-Side Validation
10
Custom Validation Rules
10
Areas
15
Asynchronous Controllers
18
Why Asynchronous Operations Matter
18
The Anatomy of an Asynchronous Controller
18
How to Implement an Async Controller
19
When to Use an Async Controller
21
RenderAction and Child Actions
22
Strongly Typed UI Helpers
24
Enhancements to ModelBinders
25
Default Value
25
Increasing Security with HTTPS
26
Avoiding the JSON Array Attack
26
Short Notation for AcceptVerbs Attribute
27
Overriding the HTTP Verb
27
Binary Binding
28
Automatic HTML-Encoding
29
Enhanced Visual Studio Tooling
30
Summary
31
About Simone Chiaretta
32
What’s New in
ASP.NET MVC 2?
Introducing ASP.NET MVC 2
ASP.NET MVC is the new Framework for building web applications released by Microsoft at the
beginning of 2009. It adopts an approach that is completely different from the traditional Web
Forms one that was introduced with the release of the .NET Framework in 2002: Instead of using
an event-based programming style, ASP.NET MVC implements the Model-View-Controller
pattern so that it’s easier to achieve a better separation of concerns and it’s easier to make the
application extensible and testable.
Version 1.0 has been downloaded and used by almost 1 million developers in the first year after its
final release, becoming more and more popular every month. But some thought that it was still too
unfinished for enterprise development, mainly because it was missing some important features
like client-side validation. In March 2010, after one year, version 2 of the Framework has been
released, whose main focus is to enhance productivity and make the Framework ready for
enterprise development.
ASP.NET MVC 2 is built on top of the first release and is completely compatible with it, which
means that all of your knowledge, skills, and even the code you wrote continue to work when you
move forward to the new version.
ASP.NET MVC 2 adds the following new features, which will be covered in detail through the rest
of this Wrox Blox:
❏
Templated Helpers
— Make it easier and faster to create the HTML to display and edit data.
❏
Client-Side Validation
— Brings the same powerful metadata-based validation to the client.
❏
Areas
— Allow you to organize a big application into smaller logical sections.
What’s New in ASP.NET MVC 2?
❏
Asynchronous Controllers
— Solve the same issue addressed by the asynchronous pages in
traditional Web Forms by performing long-running tasks on a separate thread to free up
resources on the server.
❏
Child Actions
— Make easy encapsulation of logic and presentation possible.
❏
Strongly Typed UI Helpers
— Add a new strongly typed flavor to the existing UI Helpers.
And in addition to these main features, other smaller features and enhancements to the existing ones
have been introduced, together with a better Visual Studio (VS) integration, especially in VS2010.
To make the migration to the new version easier, ASP.NET MVC 2 can be installed side-by-side with
ASP.NET MVC 1.0, which means that on the same server you can have applications written in V1 and
others written in V2, making it easier to adopt the new version for new applications while taking your
time to upgrade the older ones.
This Wrox Blox will not discuss what the ASP.NET MVC Framework is and how it works, but it is meant
to guide a developer who is already familiar with the first version of the Framework through the new
features that have been introduced into the second version. If you want to learn the basics of the
Framework or if you find yourself struggling with some concepts while you are reading this Wrox Blox,
I suggest that you read one of the books that treat the first version — such as
Beginning ASP.NET
MVC 1.0
by myself and Keyvan Nayyeri (Wrox, 2009; ISBN: 978-0-470-43399-7) or
Professional
ASP.NET MVC 1.0
by Rob Conery, Scott Hanselman, Phil Haack, and Scott Guthrie (Wrox, 2009;
ISBN: 978-0-470-38461-9) — and then come back to read about the new features.
Runs on Visual Studio 2008 and Visual Studio 2010
ASP.NET MVC 2 can be used in Visual Studio 2008 (VS 2008), running on .NET 3.5 Service Pack 1, and in
the newly released Visual Studio 2010 (VS 2010), running on .NET 4.
If you want to use MVC 2 with Visual Studio 2010, there is nothing you have to do: The Framework
comes in-the-box with the new Integrated Development Environment (IDE). But if you want to use
MVC 2 with Visual Studio 2008, you have to download it from
www.asp.net/mvc/
or via the Web
Platform Installer.
Other than this different way of getting the Framework, there are a few subtle differences between
running on .NET 3.5 SP1 and on .NET4 because some features are available only with .NET 4. All the
samples and code snippets will be developed with VS 2010 and .NET 4, but when the syntax is different
or when a feature is available on .NET 4, it will be made very clear in the text.
Templated Helpers and Model Metadata
With the goal to enhance productivity, Templated Helpers are probably the most important new feature
of MVC 2.
Templated Helpers automatically build the UI needed to represent or edit data based on the data model
itself and on a set of metadata that, in the default implementation, is retrieved from the attributes
applied to the Model.
What’s New in ASP.NET MVC 2?
For example, a property of the data model can be marked with an attribute that specifies that
the property represents HTML text. The Templated Helper can automatically render the text using
a user interface (UI) specifically designed for HTML code instead of just treating it as a normal
string, or it could present the user with a WYSIWYG textbox to edit the string instead of a
normal textbox.
All the magic is done via the following HTML Helpers:
❏
Html.EditorFor — Builds the UI to edit each property in the object specified as the parameter.
❏
Html.DisplayFor — Builds the UI to display the value of each property in the object specified
as the parameter.
These two methods accept a lambda expression as the parameter, but if you prefer, there are also another
two flavors:
❏
Html.Editor
and
Html.Display
— Do the exact same thing as the
EditorFor
and
DisplayFor
, but these are used if you prefer to pass the name of the object as a string instead
of as an expression.
❏
Html.EditorForModel
and
Html.DisplayForModel
— Works directly on the full Model
of the View.
Listing 1
shows how to write the form to edit the Model of a View.
Listing 1:
The Edit View using the Templated Helpers
<% using (Html.BeginForm()) {%>
<fieldset>
<legend>Fields</legend>
<%: Html.EditorForModel() %>
<p>
<input type=”submit” value=”Save” />
</p>
</fieldset>
<% } %>
Listing 2
contains the definition of the View Model, with the attributes applied to its properties.
Listing 2:
The Post View Model
using System.Web.Mvc;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel;
public class Post
continued
Plik z chomika:
krupix.one
Inne pliki z tego folderu:
Apress Applied ASP.NET 4 in Context (2011).pdf
(62145 KB)
Addison-Wesley Essential ASP.NET with Examples in Visual Basic .NET (2003).pdf
(6439 KB)
Addison-Wesley Advanced ASP.NET AJAX Server Controls, For .NET Framework 3.5 (2009).pdf
(5332 KB)
Addison-Wesley Maximizing ASP.NET, Real World Object-Oriented Development (2005).pdf
(5641 KB)
Addison-Wesley Professional C# Developer's Guide to ASP.NET XML and ADO.NET (2002).pdf
(4564 KB)
Inne foldery tego chomika:
ASPNET_2
Zgłoś jeśli
naruszono regulamin