Siêu thị PDFTải ngay đi em, trời tối mất

Thư viện tri thức trực tuyến

Kho tài liệu với 50,000+ tài liệu học thuật

© 2023 Siêu thị PDF - Kho tài liệu học thuật hàng đầu Việt Nam

Professional ASP.NET MVC 1.0
PREMIUM
Số trang
196
Kích thước
13.8 MB
Định dạng
PDF
Lượt xem
1110

Professional ASP.NET MVC 1.0

Nội dung xem thử

Mô tả chi tiết

Professional ASP.NET MVC 1.0

Published by

Wiley Publishing, Inc.

10475 Crosspoint Boulevard

Indianapolis, IN 46256

www.wiley.com.

Copyright © 2009 by Wiley Publishing, Inc., Indianapolis, Indiana

Chapter 1 is licensed under the terms of the Creative Commons Attribution No Derivatives 3.0 license.

Published simultaneously in Canada

ISBN: 978-0-470-38461-9

Manufactured in the United States of America

10 9 8 7 6 5 4 3 2 1

Library of Congress Cataloging-in-Publication Data is available from the publisher.

No part of this publication may be reproduced, stored in a retrieval system or transmitted in any form

or by any means, electronic, mechanical, photocopying, recording, scanning or otherwise, except as

permitted under Sections 107 or 108 of the 1976 United States Copyright Act, without either the prior

written permission of the Publisher, or authorization through payment of the appropriate per-copy

fee to the Copyright Clearance Center, 222 Rosewood Drive, Danvers, MA 01923, (978) 750-8400, fax

(978) 646-8600. Requests to the Publisher for permission should be addressed to the Permissions

Department, John Wiley & Sons, Inc., 111 River Street, Hoboken, NJ 07030, (201) 748-6011, fax

(201) 748-6008, or online at http://www.wiley.com/go/permissions.

Limit of Liability/Disclaimer of Warranty: The publisher and the author make no representations or war￾ranties with respect to the accuracy or completeness of the contents of this work and specifically disclaim

all warranties, including without limitation warranties of fitness for a particular purpose. No warranty

may be created or extended by sales or promotional materials. The advice and strategies contained herein

may not be suitable for every situation. This work is sold with the understanding that the publisher is not

engaged in rendering legal, accounting, or other professional services. If professional assistance is required,

the services of a competent professional person should be sought. Neither the publisher nor the author

shall be liable for damages arising herefrom. The fact that an organization or Website is referred to in this

work as a citation and/or a potential source of further information does not mean that the author or the

publisher endorses the information the organization or Website may provide or recommendations it may

make. Further, readers should be aware that Internet Websites listed in this work may have changed or dis￾appeared between when this work was written and when it is read.

For general information on our other products and services please contact our Customer Care Department

within the United States at (877) 762-2974, outside the United States at (317) 572-3993 or fax (317) 572-4002.

Trademarks: Wiley, the Wiley logo, Wrox, the Wrox logo, Wrox Programmer to Programmer, and related

trade dress are trademarks or registered trademarks of John Wiley & Sons, Inc. and/or its affiliates, in the

United States and other countries, and may not be used without written permission. All other trademarks

are the property of their respective owners. Wiley Publishing, Inc., is not associated with any product or

vendor mentioned in this book.

Wiley also publishes its books in a variety of electronic formats. Some content that appears in print may not

be available in electronic books.

84619ffirsw.indd 6 3/3/09 8:30:50 AM

Professional ASP.NET MVC 1.0

Table of Contents

Chapter 1: NerdDinner.

Chapter 2: Model View Controller and ASP.NET.

Chapter 3: ASP.NET > ASP.NET MVC.

Chapter 4: Routes and URLs.

Chapter 5: Controllers.

Chapter 6: Views.

Chapter 7: AJAX.

Chapter 8: Filters.

Chapter 9: Securing Your Application.

Chapter 10: Test Driven Development With ASP.NET MVC.

Chapter 11: Testable Design Patterns.

Chapter 12: Best of Both Worlds: Web Forms and MVC Together.

Page 2 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

Chapter Contents

NerdDinner............................................................................................................ 3

File->New Project................................................................................................... 8

Creating the Database ......................................................................................... 17

Building the Model .............................................................................................. 26

Controllers and Views.......................................................................................... 42

Create, Update, Delete Form Scenarios............................................................... 67

ViewData and ViewModel ................................................................................. 101

Partials and Master Pages.................................................................................. 108

Paging Support................................................................................................... 118

Authentication and Authorization...................................................................... 127

AJAX Enabling RSVPs Accepts............................................................................. 138

Integrating an AJAX Map.................................................................................... 146

Unit Testing ....................................................................................................... 165

NerdDinner Wrap Up......................................................................................... 186

Page 3 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

NerdDinner

The best way to learn a new framework is to build something with it. This first chapter walks through

how to build a small, but complete, application using ASP.NET MVC, and introduces some of the core

concepts behind it.

The application we are going to build is called “NerdDinner”. NerdDinner provides an easy way for

people to find and organize dinners online:

NerdDinner enables registered users to create, edit and delete dinners. It enforces a consistent set of

validation and business rules across the application:

Page 4 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

Visitors to the site can search to find upcoming dinners being held near them:

Page 5 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

Clicking a dinner will take them to a details page where they can learn more about it:

If they are interested in attending the dinner they can login or register on the site:

Page 6 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

They can then easily RSVP to attend the event:

Page 7 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

We are going to begin implementing the NerdDinner application by using the File->New Project

command within Visual Studio to create a brand new ASP.NET MVC project. We’ll then incrementally

add functionality and features. Along the way we’ll cover how to create a database, build a model with

business rule validations, implement data listing/details UI, provide CRUD (Create, Update, Delete) form

entry support, implement efficient data paging, reuse UI using master pages and partials, secure the

application using authentication and authorization, use AJAX to deliver dynamic updates and interactive

map support, and implement automated unit testing.

You can build your own copy of NerdDinner from scratch by completing each step we walkthrough in

this chapter. Alternatively, you can download a completed version of the source code here:

http://tinyurl.com/aspnetmvc.

You can use either Visual Studio 2008 or the free Visual Web Developer 2008 Express to build the

application. You can use either SQL Server or the free SQL Server Express to host the database.

You can install ASP.NET MVC, Visual Web Developer 2008, and SQL Server Express using the Microsoft

Web Platform Installer available here: http://www.microsoft.com/web/downloads

Page 8 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

File->New Project

We’ll begin our NerdDinner application by selecting the File->New Project menu item within Visual

Studio 2008 or the free Visual Web Developer 2008 Express.

This will bring up the “New Project” dialog. To create a new ASP.NET MVC application, we’ll select the

“Web” node on the left-hand side of the dialog and then choose the “ASP.NET MVC Web Application”

project template on the right:

We’ll name the new project “NerdDinner” and then click the “ok” button to create it.

When we click "ok" Visual Studio will bring up an additional dialog that prompts us to optionally create a

unit test project for the new application as well. This unit test project enables us to create automated

tests that verify the functionality and behavior of our application (something we’ll cover how to-do later

in this tutorial).

Page 9 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

The "Test framework" dropdown in the above dialog is populated with all available ASP.NET MVC unit

test project templates installed on the machine. Versions can be downloaded for NUnit, MBUnit, and

XUnit. The built-in Visual Studio Unit Test framework is also supported.

Note: The Visual Studio Unit Test Framework is only available with Visual Studio 2008 Professional and

higher versions). If you are using VS 2008 Standard Edition or Visual Web Developer 2008 Express you

will need to download and install the NUnit, MBUnit or XUnit extensions for ASP.NET MVC in order for

this dialog to be shown. The dialog will not display if there aren’t any test frameworks installed.

We'll use the default "NerdDinner.Tests" name for the test project we create, and use the “Visual Studio

Unit Test” framework option. When we click the "ok" button Visual Studio will create a solution for us

with two projects in it - one for our web application and one for our unit tests:

Page 10 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

Examining the NerdDinner directory structure

When you create a new ASP.NET MVC application with Visual Studio, it automatically adds a number of

files and directories to the project:

ASP.NET MVC projects by default have six top-level directories:

Directory Purpose

/Controllers Where you put Controller classes that handle URL requests

/Models Where you put classes that represent and manipulate data

/Views Where you put UI template files that are responsible for rendering output

/Scripts Where you put JavaScript library files and scripts (.js)

/Content Where you put CSS and image files, and other non-dynamic/non-JavaScript content

/App_Data Where you store data files you want to read/write.

ASP.NET MVC does not require this structure. In fact, developers working on large applications will

typically partition the application up across multiple projects to make it more manageable (for example:

data model classes often go in a separate class library project from the web application). The default

project structure, however, does provide a nice default directory convention that we can use to keep

our application concerns clean.

When we expand the /Controllers directory we’ll find that Visual Studio added two controller classes –

HomeController and AccountController – by default to the project:

Page 11 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

When we expand the /Views directory, we’ll find three sub-directories – /Home, /Account and /Shared –

as well as several template files within them were also added to the project by default:

Page 12 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

When we expand the /Content and /Scripts directories, we’ll find a Site.css file that is used to style all

HTML on the site, as well as JavaScript libraries that can enable ASP.NET AJAX and jQuery support within

the application:

When we expand the NerdDinner.Tests project we’ll find two classes that contain unit tests for our

controller classes:

Page 13 of 186

Chapter 1 is licensed under the terms of Creative Commons Attribution No Derivatives 3.0 license and may be redistributed according to those terms with the

following attribution: “Chapter 1 “NerdDinner” from Professional ASP.NET MVC 1.0 written by Rob Conery et al published by Wrox (ISBN: 978-0-470-38461-9) may

be redistributed under the terms of Creative Commons Attribution No Derivatives 3.0 license. The original copy is available at http://tinyurl.com/aspnetmvc. The

complete book Professional ASP.NET MVC 1.0 is copyright 2009 by Wiley Publishing Inc and may not be redistributed without permission.”

These default files added by Visual Studio provide us with a basic structure for a working application -

complete with home page, about page, account login/logout/registration pages, and an unhandled error

page (all wired-up and working out of the box).

Running the NerdDinner Application

We can run the project by choosing either the Debug->Start Debugging or Debug->Start Without

Debugging menu items:

This will launch the built-in ASP.NET Web-server that comes with Visual Studio, and run our application:

Tải ngay đi em, còn do dự, trời tối mất!