索引:
一.创建一个空项目
请查看 新建 .NET Core 项目 -- Hello World! 一节,新建一个项目:
二.添加引用并修改配置为 MVC
修改 .vscode\launch.json 文件
代码如下:
{
"version": "0.2.0",
"configurations": [
{
"name": ".NET Core Launch (web)",
"type": "coreclr",
"request": "launch",
"preLaunchTask": "build",
"program": "${workspaceRoot}\\bin\\Debug\\netcoreapp1.0\\WebAppCore.dll",
"args": [],
"cwd": "${workspaceRoot}",
"stopAtEntry": false,
"internalConsoleOptions": "openOnSessionStart",
"env": {
"ASPNETCORE_ENVIRONMENT": "Development"
},
"sourceFileMap": {
"/Views": "${workspaceRoot}/Views" // 用来编译 cshtml
}
}
]
}
launch.json
修改 .vscode\tasks.json 文件
代码如下:
{
"version": "0.1.0",
"command": "dotnet",
"isShellCommand": true,
"args": [],
"tasks": [
{
"taskName": "build",
"args": [
"${workspaceRoot}\\project.json"
],
"isBuildCommand": true,
"problemMatcher": "$msCompile"
}
]
}
tasks.json
修改 project.json 项目文件
代码如下,注意必要依赖的添加项,微软新的MVC库文件:
{
"dependencies": {
"Microsoft.NETCore.App": { // 多平台编译,必须在这里指明 .net core
"version": "1.0.1",
"type": "platform"
},
"Microsoft.ApplicationInsights.AspNetCore": "1.0.0",
"Microsoft.AspNetCore.Mvc": "1.0.1",
"Microsoft.AspNetCore.Razor.Tools": {
"version": "1.0.0-preview2-final",
"type": "build"
},
"Microsoft.AspNetCore.Routing": "1.0.1",
"Microsoft.AspNetCore.Server.Kestrel": "1.0.1"
}, "tools": {
"Microsoft.AspNetCore.Razor.Tools": "1.0.0-preview2-final"
},
"frameworks": {
"netcoreapp1.0": {
"imports": [
"dotnet5.6"
]
}
},
"buildOptions": {
"emitEntryPoint": true,
"preserveCompilationContext": true
},
"runtimeOptions": {
"configProperties": {
"System.GC.Server": true
}
}
}
project.json
添加 Startup.cs 文件
代码如下,注意代码中的 ConfigureServices 与 Configure 方法:
using Microsoft.AspNetCore.Builder;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection; namespace WebAppCore
{
public class Startup
{
public IConfigurationRoot Configuration { get; } public Startup()
{
Configuration = new ConfigurationBuilder().Build();
} // 被 runtime 使用的方法.
// 用这个方法向 容器 中添加服务.
public void ConfigureServices(IServiceCollection services)
{
services.AddApplicationInsightsTelemetry(Configuration);
services.AddMvc(); } // 被 runtime 使用的方法.
// 用这个方法配置 Http 请求管道.
public void Configure(IApplicationBuilder app)
{
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=HelloWorld}/{action=Index}/{id?}");
});
}
}
}
Startup.cs
修改 Program.cs 文件
代码如下,代码中建立了 Host :
using System.IO;
using Microsoft.AspNetCore.Hosting; namespace WebAppCore
{
public class Program
{
public static void Main(string[] args)
{
var host = new WebHostBuilder()
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseStartup<Startup>()
.Build(); host.Run();
}
}
}
Program.cs
三.添加 Controller/View
在项目中分别添加以下文件夹
Controllers
Views
Views\HelloWorld
Views\Shared
在 Views 目录中创建 模板页及相关文件
Views\Shared\_Layout.cshtml
Views\_ViewImports.cshtml
Views\_ViewStart.cshtml
代码如下,注意布局页的使用:
<!DOCTYPE html> <html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"]</title>
</head>
<body>
<div class="container body-content">
@RenderBody()
</div>
</body>
</html>
_Layout.cshtml
@addTagHelper *, Microsoft.AspNetCore.Mvc.TagHelpers
_ViewImports.cshtml
@{
Layout = "_Layout";
}
_ViewStart.cshtml
添加控制器 Controllers\HelloWorldController.cs
添加与控制器对应的视图 Views\HelloWorld\Index.cshtml
相应代码如下:
using Microsoft.AspNetCore.Mvc; namespace WebAppCore.Controllers
{
public class HelloWorldController : Controller
{
public IActionResult Index()
{
//
return View();
}
}
}
HelloWorldController.cs
@{
ViewData["Title"] = "Index";
Layout = "~/Views/Shared/_Layout.cshtml";
} <h2>Hello World!</h2>
Index.cshtml
四.使用Visual Studio Code 运行
点击 运行
输出窗口可看到,编译结果:
调试窗口可看到,站点已启动:
在浏览器中,请求地址 http://localhost:5000/helloworld/index 如下:
调试窗口,可看到:
调试执行,浏览器中可看到:
蒙
2016-09-21 14:12 周三