没有合适的资源?快使用搜索试试~ 我知道了~
ASP.NET MVC 2 in Action 2nd
需积分: 1 30 下载量 10 浏览量
2010-06-23
15:24:55
上传
评论
收藏 8.67MB PDF 举报
温馨提示
试读
433页
ASP.NET MVC 2 in Action 2nd,不是最终的版本,是通过作者手稿整理,并且手工添加了索引,便于了解内容的概貌,便于阅读定为。随书源码作者放在了 GitHub 上,可以搜索下载。
资源推荐
资源详情
资源评论
1
High-speed beginner ramp-up
This chapter covers
Introducing the MVC pattern
Dissecting the default application template
Creating your first ASP.NET MVC 2 project
Handling user input
Working with the view
This chapter is intended to provide you with a quick, high-level overview of the ASP.NET MVC
Framework. We’ll create a basic sample application, collect user input, and display some web
pages.
But first, let me introduce you to your new friend…
1.1 Welcome to ASP.NET MVC
ASP.NET MVC is a new web application framework from Microsoft. It was first unveiled in
November 2007 and has since seen more than 10 releases and 2 major versions. With the
high number of releases, this framework has received quite a bit of feedback and is much
more stable than some other new frameworks from Microsoft, such as Windows Workflow
Foundation. MVC stands for Model-View-Controller, a pattern that’s becoming increasingly
popular with web development frameworks.
ASP.NET MVC is both an alternative and a complement to Web Forms, which means you
won’t be dealing with pages and controls, postbacks or view state, or complicated event
lifecycles. Instead, you’ll be defining controllers, actions, and views. The underlying ASP.NET
platform is the same, however, so things like HTTP handlers and HTTP modules still apply,
and you can mix MVC and Web Forms pages in the same application.
We’ll cover all the major features of the framework throughout this book. Here are some
of the benefits you’ll learn about:
Full control over HTML
Full control over URLs
Better separation of concerns
Extensibility
Testability
As you read the chapters in this book, these benefits will become increasingly apparent.
For now, we’ll briefly look at the underlying pattern the framework is based on. Why MVC?
Where did it come from?
1.2 The MVC pattern
The Model-View-Controller (MVC) pattern is an adaptation of a pattern generated from the
Smalltalk community in the 1970s by Trygve Reenskaug. It was popularized for use on the
web with the advent of Ruby on Rails in 2003.
The components of MVC are straightforward:
The model—The “thing” that your software is built around. If you were building a blog,
your models might be post and comment. In some contexts, this might refer to a
view-specific model, which you’ll learn about in the next chapter.
The view—A visual representation of a model, given some context. It’s usually the
resulting HTML that the framework renders to the browser, such as the HTML
representing the blog post.
The controller—A mediator. The controller processes input, acts upon the model, and
decides what to do—render a view, redirect somewhere else, and so on. The controller
might pull the most recent comments for a blog post and send them to a view.
To see how these components interact with each other, take a look at figure 1.1.
Figure 1.1 The relationship between the model, view, and controller. The solid lines indicate a direct
association, and the dashed lines indicate an indirect association.
Now that you have a rudimentary overview of the ASP.NET MVC Framework and the MVC
pattern in general, you’re armed to create your first project.
1.3 Creating your first ASP.NET MVC 2 project
We’ll create a web application with some guestbook features. Fire up Visual Studio, and go to
File > New Project. You’re presented with the dialog box pictured in figure 1.2.
NOTE
The rest of this book assumes that you have ASP.NET MVC 2 installed, either on Visual
Studio 2008 or on Visual Studio 2010. If you don’t, check the appendix for instructions on
how to obtain it.
Figure 1.2 The New Project dialog box. Notice the ASP.NET MVC 2 project templates.
In the left pane, under Project Types, select Web. In the Templates pane, select ASP.NET
MVC 2 Web Application. Give the application a name and location, and click OK.
You’re greeted with a dialog box (figure 1.3) that asks you if you want to create a unit
test project. Normally we’d recommend creating a unit test project because most nontrivial
projects need automated tests, but to keep this chapter focused, we’ll select No for now.
Figure 1.3 Visual Studio prompts you to create a unit test project. For now, select No.
Your project is ready to go. Visual Studio created a number of folders for you. Let’s
examine them and see what their purposes are:
Content—Static files such as CSS and images
Controllers—Your application’s controller classes
Models—Your application’s models
Scripts—JavaScript files
Views—Your application’s views
Take a look at the folder structure for a minute. You’ll work with this structure for all your
ASP.NET MVC projects, so everything will eventually look familiar.
The application that Visual Studio has given you is a working sample of the ASP.NET MVC
Framework. That means you can just run it (Ctrl-F5) to see how it works. Go ahead and do
that now.
Your browser should be opened, and you should be looking at a page that looks like
figure 1.4. Notice that the URL is simply http://localhost:port/. No path is specified. Let’s
examine how this view was rendered.
Figure 1.4 The default ASP.NET MVC project template is fully functional.
The initial request to the application was made to / (the root of the site). We can check
the routes to see how the application responds to URLs. Routes are a way for you to
customize the URLs that users use when interacting with your site. You’ll learn about routing
in depth in chapter 16, but we’ll cover what you need to know to get started.
Routes are (by default) defined in the Global.asax. Open this file and you should see the
code shown in listing 1.1.
剩余432页未读,继续阅读
资源评论
Proteas
- 粉丝: 120
- 资源: 32
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功