有些开发者喜欢用Visual Studio。
本烹饪宝典介绍了在Visual Studio 2015的ASP.NET 4.x项目中,用Angular实现“快速起步”所需的步骤。
本烹饪宝典中没有在线例子,因为它介绍的是Visual Studio,而不是《快速起步》应用程序本身。
ASP.NET 4.x 项目
ASP.NET 4.x Project
要用Visual Studio 2015在ASP.NET 4.x项目中设置《快速起步》文件,请遵循如下步骤:
-
前提条件: 安装Node.js
-
前提条件: 安装Visual Studio 2015 Update 3
-
前提条件: 配置External Web tools
-
前提条件: 安装TypeScript 2 for Visual Studio 2015
-
第一步: 下载“快速起步”的文件
-
第二步: 创建Visual Studio ASP.NET项目
-
第三步: 把“快速起步”中的文件拷贝到ASP.NET的项目目录中
-
第四步: 恢复需要的包
-
第五步: 构建和运行应用程序
如果你希望使用ASP.NET Core并体验全新项目, 参见预览版ASP.NET Core + Angular template for Visual Studio 2015。 注意,最终代码与本文不对应,请适当调节。
前提条件: Node.js
如果你的电脑里没有Node.js®和npm,请安装它们。
在终端或者控制台中运行node -v
和npm -v
,请确认你的Node版本为4.6.x
或更高,npm的版本为3.x.x
或更高。老版本会引起错误。
前提条件: Visual Studio 2015 Update 3
使用Visual Studio开发Angular应用程序的最低要求是Update 3。
早期版本没有遵循使用TypeScript开发应用程序的最佳实践。
要查看你的Visual Studio 2015版本号,到Help | About Visual Studio
。
如果还没有,安装Visual Studio 2015 Update 3。或者使用Tools | Extensions and Updates
来直接在Visual Studio 2015中更新到Update 3。
前提条件: 配置External Web tools
配置Visual Studio来使用全局External Web Tools,而非Visual Studio默认的工具:
-
到
Tools
|Options
打开Options对话框
-
在左边树型项目中,选择
Projects and Solutions
|External Web Tools
。
On the right, move the
$(PATH)
entry above the$(DevEnvDir
) entries. This tells Visual Studio to use the external tools (such as npm) found in the global path before using its own version of the external tools.在右侧,将
$(PATH)
移动到$(DevEnvDir
)上面。这样,Visual Stuio就会在使用自带的外部工具时,优先使用全局路径中的外部工具(比如npm)。Click OK to close the dialog.
点击OK关闭对话框。
Restart Visual Studio for this change to take effect.
- 重启Visual Studio,以让设置变化生效。
Visual Studio将优先在当前的工作区查找外部工具,如果没有找到,便查找全局路径,如果还没有找到,Visual Studio就使用自带的工具版本。
前提条件: 安装TypeScript 2.2 for Visual Studio 2015
Visual Studio Update 3自带TypeScript支持,但是它的TypeScript版本开发Angular应用需要的不是2.2。
要安装TypeScript 2.2:
-
或通过npm安装:
npm install -g typescript@2.2
。
你可以在这儿查看更多Visual Studio中TypeScript 2.2的支持。
至此,Visual Studio准备好了。重新启动Visual Stuido,这样我们可以有一个崭新的开始。
第一步: 现在“快速起步”文件
从GitHub下载“快速起步”的源代码。如果下载的是一个压缩的zip文件,解压它。
第二步:创建Visual Studio ASP.net项目
按照下列步骤创建ASP.NET 4.x项目:
-
在Visual Studio中,选择
File
|New
|Project
菜单。
-
在模板树中,选择
Templates
|Visual C#
(或Visual Basic
) |Web
菜单。
-
选择
ASP.NET Web Application
模板,输入项目名,点击“OK”按钮。
-
选择自己喜欢的ASP.NET 4.5.2模板,点击OK。
本烹饪宝典选择了Empty
模板,它没有添加过任何目录,没有身份验证,没有服务器托管。为你的项目选择合适的模板和选项。
第三步: 把“快速起步”的文件复制到ASP.NET项目所在的目录
Step 3: Copy the QuickStart files into the ASP.NET project folder
拷贝从GitHub下载的“快速起步”文件到包含.csproj
文件的目录中。按照下面的步骤把它们加到Visual Studio中:
-
在Solution Explorer中点击
Show All Files
按钮,显示项目中所有隐藏文件。
-
右键点击每个目录和文件,选择
Include in Project
。 最少要添加下列文件:-
src/app目录(如果询问是否要搜索TypeScript类型,回答No)
-
- src/styles.css
- src/index.html
- package.json
src/tsconfig.json
typings.json
第四步: 恢复需要的包
按下面的步骤恢复Angular应用程序需要的包:
-
在Solution Explorer中右键点击
package.json
,选择Restore Packages
。
这样,Visual Studio会使用npm
来安装在package.json
中定义的所有包. 这可能需要花一点时间。 -
如果愿意,打开Output窗口(
View
|Output
)来监控npm命令的执行情况。 -
忽略所有警告。
-
当恢复完成后,将会出现一条消息:
Installing packages complete
。耐心点,这相当耗时间。 -
在Solution Explorer里,点击
Refresh
图标。 -
不要将
node_modules
目录添加到项目中,让它隐藏。
第五步:构建和运行应用
Step 5: Build and run the app
首先,确认src/index.html
已被设置为开始页面。
在Solution Explorer中,右键点击index.html
,选择选项Set As Start Page
。
To run in VS with F5
Most Visual Studio developers like to press the F5 key and see the IIS server come up. To use the IIS server with the QuickStart app, you must make the following three changes.
- In
index.html
, change base href from<base href="/">
to<base href="/src/">
. - Also in
index.html
, change the scripts to use/node_modules
with a slash instead ofnode_modules
without the slash. - In
src/systemjs.config.js
, near the top of the file, change the npmpath
to/node_modules/
with a slash.
After these changes, npm start
no longer works.
You must choose to configure either for F5 with IIS or for npm start
with the lite-server.
For apps that use routing
If your app uses routing, you need to teach the server to always return
index.html
when the user asks for an HTML page
for reasons explained in the Deployment guide.
Everything seems fine while you move about within the app. But you'll see the problem right away if you refresh the browser or paste a link to an app page (called a "deep link") into the browser address bar.
You'll most likely get a 404 - Page Not Found response from the serer
for any address other than /
or /index.html
.
You have to configure the server to return index.html
for requests to these "unknown" pages.
The lite-server
development server does out-of-the-box.
If you've switched over to F5 and IIS, you have to configure IIS to do it.
This section walks through the steps to adapt the QuickStart application.
Configure IIS rewrite rules
Visual Studio ships with IIS Express, which has the rewrite module baked in. However, if you're using regular IIS you'll have to install the rewrite module.
Tell Visual Studio how to handle requests for route app pages by adding these
rewrite rules near the bottom of the web.config
:
<system.webServer>
<rewrite>
<rules>
<rule name="Angular Routes" stopProcessing="true">
<match url=".*" />
<conditions logicalGrouping="MatchAll">
<add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
<add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
</conditions>
<action type="Rewrite" url="/src/" />
</rule>
</rules>
</rewrite>
</system.webServer>
The match url, <match url=".*" />
, will rewrite every request. You'll have to adjust this if
you want some requests to get through, such as web API requests.
The URL in <action type="Rewrite" url="/src/"/>
should
match the base href in index.html
.
点击Run按钮或者按F5
键,用调试器构建和启动应用。
按Ctrl-F5
不带调试器的运行应用,速度会更快。
默认浏览器打开并显示《快速起步》例子应用。
尝试编辑任何项目文件,保存并刷新浏览器来查看效果。