ASP.NET MVC 4 简介
本教程将教你如何基于微软 Visual Studio Express 2012 或 Visual Web Developer 2010 Express Service Pack 1 构建ASP.NET MVC 4网站应用程序。推荐使用VS2012,你不需要安装任何东西就可以完成本教程。如果你使用VS2010,那你必须安装以下组件,全部安装它们你可以点击以下链接:
Tthis tutorial will teach you the basics of building an ASP.NET MVC 4 Web application using Misrosoft Visual Studio Express 2012 or Visual Web Developer 2012 Express Service Pack 1. Visual Studio 2012 is recommended, you won't need to install anything to compete the tutorial. If you are using Visual Studio 2010 you must install the components below. You can install all of them by clicking the following links:
如果你使用Visual Studio 2020 替代 Visual Web Developer 2010, 安装 WPI installer for ASP.NET MVC 4 和 Visual Studio 2010 prerequisites
If you're using Visual Studio 2010 instead of Visual Web Developer 2010,install the WPI installer for ASP.NET MVC 4 and the : Visual Studio 2010 prerequisites
这里有一个可用的 Visual Web Developer C# 项目源码,可查看主题. Download the C# version.
AVisual Web Developer project with C# source code is available to accompany this topic.Download the C# version.
你将构建什么? What You'll Build
你将实现一个支持创建、编辑、查询和从数据中查询影片列表的简单电影列表应用程序。下面的两张截图就是你将要实现的应用程序。它包括一个从数据库中读出的影片列表页面。
You'll implement a simple movie-listing application that supports creating,editing,searching and listing movies from a database. Below are two screenshots of the application you'll buid. It includes a page that displays a list of movies from a database:
这个程序也让你添加、编辑和删除影片,以及查看个人信息。所有的数据输入都包含验证以确保它们存储在数据库中是正确的。
The application also lets you add,edit,and delete movies, as well as see details about individual ones. All data-entry scenarios include validation to ensure that the data stored in the database is correct.
入门 Getting Started
打开 VS 2012 Express 或 Visual Web Developer 2010 Express. 通常本系列教程的截图都是使用 VS 2010 Express ,但是你能通过 VS2010/SP1、VS2012、VS2012 Express 或 Visual Web Developer完成本教程。从【开始页面】选择【新建项目】。
Start by running Visual Studio Express 2012 or Visual Web Developer 2010 Express. Most of the screen shots in this series use Visual Studio Express 2012, but you can complete this tutorial with Visual Studio 2010/SP1, Visual Studio 2012, Visual Express 2012 or Visual Web Developer 2010 Express. Select New Project from the Startpage.
VS 是一个集成开发环境,只要你喜欢使用 MS Word 编写文档,你就能使用IDE创建一个程序。在VS中你可看各种可用的选项显示在的顶部的工具条里。IDE还提供另外执行任务的菜单。(如:替换从开始页面选择的新建项目,你可以从FILE菜单选择新建项目)
Visual Studio is an IDE, or integrated development environment. Just like you use Microsoft Word to write document, you'll use an IDE to create applications. In Visual Studio there's a toolbar along the top showing various options available to you. There's also a menu that provides another way to perform task in the IDE.(For example, instead of selecting New Project from the Start page, you can use the menu and select File > New Project.)
创建你的第一个程序 Creating Your First Application
你可以使用VB 或C#编程语言创建程序。在左边菜单中选择 ASP.NET 网站应用程序。给你的项目名称起为“MvcMovie” 并点击 确定。
You can create applications using either Visual Basic or Visual C# as the programming language. Select Visual C# on the left and then select ASP.NET MVC 4 Web Application. Name your project "MvcMovie" and then click OK.
在新ASP.NET MVC 4 项目对话框中,选择网联网应用。保持 Razor为默认的视图引擎。
In the New ASP.NET MVC 4 Project dialog box, Select Internet Application. Leave Razor as the default view engine.
点击 确定。VS 使用将只使用默认模板为你创建ASP.NET MVC 项目,就这样你现在就有一个为你做任何事的程序了!这只是一个简单的“Hello World!”项目,但它是开启你应用程序的启点。
Click OK. Visual Studio used a default template for the ASP.NET MVC project you justcreated, so you have a working application right now without doing anything! This is a simple "Hello World!" project, and it's a good place to start your application.
从Debug菜单,选择 开始调试。
From the Debug menu, select Start Debugging.
提示:键盘 F5是开始调试的快捷键。
Notice that the keyboard shortcut to start debugging is F5.
按F5键 VS 将启动 IIS Express并运行你的程序。VS接下来会运行浏览器并打开程序的主页。注意地址栏里是 localhot 而不是像 example.com,那是因为 localhost 指向的是你的本地计算机,像这样的情况,都只是编译运行。当VS运行一个WEB项目,所有使用的都是随机端口号。如下图那样端口号就是41788。当你运行程序时你可能看到的就是不同的端口号。
F5 causes Visual Studio to start IIS express and your web application. Viisual Studio then launches a browser and opens the application's home page. Notice that the address bar of the browser says localhost and not something like example.com. That's because localhost always poingts to your own local computer, which in this case is running the application you just built. When Visual Studio runs a web project, a random port is used for the web server. In the image below, the port number is 41788. When you run the application, you'll brobably see a different port number.
窗口中给你的默认模板有 主页、联系和关于页面。它也提供支持注册和登录,还有Facebook和Twitter的链接。在接下来的步骤中将知道怎么改变这个程序的工作和学习ASP.NET MVC的一小部分知识。关闭你的浏览器让我们一起改变一些代码吧。
Right out of the box this default template gives you Home,Contact and About pages. It also provides support to register and log in, and links to Facebook and Twitter. The next step is to change how this application works and learn a little bit about ASP.NET MVC. Close your browser and let's change some code.