[.NET Core].NET Core R2安装教程及Hello示范
前言
前几天.NET Core发布了.NET Core 1.0.1 R2 预览版,之前想着有时间尝试下.NET Core。由于各种原因,就没有初试。刚好,前几天看到.NET Core发布新版本了,决定要去一探究竟。于是乎,就立马去官网查找相关的信息,为初探做准备。
下面就开始今天的内容,有两个部分:安装和创建示例程序。
安装
本人使用的是Windows 10 64位系统,安装过Visual Studio 2015,如果没有安装,请先安装。
下载安装文件
进入.NET Core官网,进入下载页面1,进入下载页面2,下载所需的安装文件。
需要下载的文件:
- .NET Core Installer(RC2)
- .NET Core SDK Installer(Preview 1)
- Windows (Server Hosting)
- DotNetCore.1.0.0.RC2-VS2015Tools
- NuGet Manager extension for Visual Studio
Windows系统直接下载安装文件即可。
Windows (Server Hosting)的作用相当于iis,是.NET Core Web项目的服务宿主程序,即可以直接使用Server Hosting运行Web项目。
You probably only need to download one of these:
- .NET Core = Run apps with .NET Core runtime
- .NET Core SDK = Develop apps with .NET Core and the SDK+CLI (Software Development Kit/Command Line Interface) tools
只需要安装下面其中一个就可以:
- .NET Core = .NET Core运行时
- .NET Core SDK = .NET Core运行时 + .NET Core开发套件(oftware Development Kit) + .NET Core 命令行工具(Command Line Interface)
安装.NET Core
提示:请先卸载.NET Core之前的版本,否则会报错。
报错信息:
The project is configured to use .NET Core SDK version 1.0.0-preview1-002702 which is not installed or cannot be found under the path C:\Program Files\dotnet\bin. These components are required to build and run this project. NetCoreR2.Sample.ConsoleApp
双击下载的DotNetCore.1.0.0.RC2-Runtime-x64.exe,选择同意协议,然后点击"Install"安装,等待安装结束。
安装.NET Core SDK
双击下载的DotNetCore.1.0.0.RC2-SDK.Preview1-x64.exe,选择同意协议,然后点击"Install"安装,等待安装结束。
安装Server Hosting
双击下载的DotNetCore.1.0.0.RC2-WindowsHosting.exe,选择同意协议,然后点击"Install"安装,等待安装结束。
安装.NET Core VS2015Tools
双击下载的DotNetCore.1.0.0.RC2-VS2015Tools.Preview1.exe,选择同意协议,然后点击"Install"安装,等待安装结束。
安装NuGet Manager extension for Visual Studio
双击下载的NuGet.Tools.vsix,选择同意协议,然后点击"Install"安装,等待安装结束。
NuGet Manager extension for Visual Studio Download
示例
示例有控制台程序和ASP.NET Core Web程序。
.NET Core控制台程序
打开Visual Studio 2015,新建一个项目:文件-新建-项目
在左侧模板选择.NET Core,右侧选择控制台应用程序(.NET Core)。
输入名称NetCoreR2.Sample.ConsoleApp
,点击"确定"按钮。
OK,.NET Core控制台应用程序创建完成。
打开Program.cs文件,写入代码,运行。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
namespace NetCoreR2.Sample.ConsoleApp
{
public class Program
{
public static void Main(string[] args)
{
Console.WriteLine("Hello .NET Core 1.0.0 R2 Console App!");
Console.ReadLine();
}
}
}
如果在这里提示
ASP.NET Core Web项目
在上面的解决方案上新建一个ASP.NET Core Web项目:添加-新建项目
选择ASP.NET Core Web Application(.NET Core),点击"确定",创建项目。
选择Web 应用程序
更改身份验证为:不进行身份验证,然后确定。
创建好项目后,等待Neget包还原,然后按"F5",调试运行。可以选择IIS或WindowsHosting,在这选用后者。
接下来,自己写一个控制器,并显示信息。
创建一个HelloController控制器,添加一个Index的Action:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Mvc;
// For more information on enabling MVC for empty projects, visit http://go.microsoft.com/fwlink/?LinkID=397860
namespace NetCoreR2.Sample.WebApp.Controllers
{
public class HelloController : Controller
{
// GET: /<controller>/
public IActionResult Index()
{
ViewData["Msg"] = "Hello .NET Core 1.0.0 R2 Asp.Net Core MVC App!";
return View();
}
}
}
创建对应的视图文件,写入代码:
@{
ViewData["Title"] = "Hello Index Page";
}
<h3>@ViewData["Msg"].ToString()</h3>
本文就介绍到这里。
如有疑问请联系我。
- 12楼大大泡泡猪
- error: Failed to retrieve information from remote source #39;https://go.microsoft.com/fwlink/?LinkID=230477/FindPackagesById()?id=#39;Microsoft.NETCore.DotNetHostResolver#39;#39;.,error: Response status code does not indicate success: 302 (Moved Temporarily).,,在还原包的时候,类似的一些这种错误,,结果就是诸如:,The dependency Microsoft.AspNetCore.Server.Kestrel gt;= 1.0.0-rc2-final could not be resolved.
- 11楼codesnippet.info
- 现在都修复了。
- Re: SeayXu
- @codesnippet.info,我现在手动把全部上传到博客园了
- 10楼坐地炮
- 有在mac下安装的么?
- Re: SeayXu
- @坐地炮,基本差不多,没有mac机子,要是不嫌弃,可以远程协助。
- 9楼codesnippet.info
- http://upload-images.jianshu.io/upload_images/1628444-05ac5dac52e7ca93.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240 Failed to load resource: the server responded with a status of 403 (Forbidden),,都是缓存地址,你可以是因为你的浏览器里已经有缓存内容了。,.png之后都删除掉。
- Re: SeayXu
- @codesnippet.info,好的,谢谢。
- 8楼黑色街角
- 不错!顶一个!
- Re: SeayXu
- @黑色街角,谢谢支持
- 7楼codesnippet.info
- 简书的图,需要把后面?之后的东西都删除掉。
- Re: SeayXu
- @codesnippet.info,能不能发个图看下,我这边显示都是ok的
- 6楼苦逼师哥
- 直接安装第四个就行,他包含其余的几个,netcoresdk 也包含netcore
- 5楼suxin
- 安装DotNetCore.1.0.0.RC2-VS2015Tools 需要翻墙吗?一直提示0x80072efd 错误,安装不上
- Re: SeayXu
- @suxin,不需要的,看下22#的
- 4楼dudu
- 我试了一下,只要安装 .NET Core SDK Installer(Preview 1) 与 DotNetCore.1.0.0.RC2-VS2015Tools 就可以了。.NET Core SDK 包含了 .NET Core runtime。,,另外,文中的quot;Insteallquot;单词拼错了。
- Re: SeayXu
- @dudu,谢谢大神提示,马上修改
- Re: 大大泡泡猪
- @XY.Seay,,楼主是否清楚15楼的情况是什么原因呢?
- 3楼一起开源
- 不错,赞1个
- Re: SeayXu
- @一起开源,谢谢支持
- 2楼Simple is best
- 俺觉得 7楼 johnvwan的疑问还是没有得到解答......
- 1楼johnvwan
- 为什么左侧选择了.Net Core项目,上方还有.NET FRAMEWORK 4.6的字样?这样.NET CORE和.net framework 4.6 区别在哪儿?
- Re: SeayXu
- @johnvwan,它们两者都是基于.NET环境运行的,.NET Core 是一个开源的、跨平台的 .NET 实现。而 .NET Framework 是基于 Windows 的 .NET 实现。另外.NET Framework 版本在4.5及以上版本才支持.NET Core。
- Re: codesnippet.info
- @johnvwan,上方那个东西是.NET Framework版本号。是VS的工程的固有模板。,补充知识 .Net Framework .Net Core Mono 区别联系,http://codesnippet.info/Article/Index?ArticleId=00000023