.NET Core 2.0和ASP.NET Core 2.0正式版抢先体验

 .NET Core 2.0和ASP.NET Core 2.0正式版抢先体验

.NET Standard 2.0 is final

Broad platform support. .NET Standard 2.0 is supported on the following platforms:

  • .NET Framework 4.6.1
  • .NET Core 2.0
  • Mono 5.4
  • Xamarin.iOS 10.14
  • Xamarin.Mac 3.8
  • Xamarin.Android 7.5
  • UWP is work in progress and will ship later this year.

https://github.com/dotnet/standard/issues/439 

.NET Core 2.0 正式版NuGet库: https://dotnet.myget.org/gallery/dotnet-2-0-0-rtm

.NET Core 2.0 正式版发布时间将会在.NET Conf 上发布。具体时间为9月18日或19日。

.NET Core 2.0.1 SDK Windows x64下载

https://dotnetcli.blob.core.windows.net/dotnet/Sdk/release/2.0.0/dotnet-sdk-latest-win-x64.exe

更多版本下载:https://github.com/dotnet/cli/tree/release/2.0.0

下面来正式体验。本文使用sdk 压缩包。

新建项目

dotnet --info

 

Console

新建项目 dotnet new console

在项目目录下添加 NuGet.Config 如下:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <packageSources>
        <clear />
        <add key="dotnet-2-0-0-rtm" value="https://dotnet.myget.org/F/dotnet-2-0-0-rtm/api/v3/index.json" />
    </packageSources>
</configuration>

添加 System.Data.SqlClient 包,使用dotnet add package 或者编辑csproj。

<Project Sdk="Microsoft.NET.Sdk">

  <PropertyGroup>
    <OutputType>Exe</OutputType>
    <TargetFramework>netcoreapp2.0</TargetFramework>
  </PropertyGroup>
    <ItemGroup>
    <PackageReference Include="System.Data.SqlClient" Version="4.4.0" />
  </ItemGroup>

</Project>

接着还原包

然后使用VS Code 打开文件夹,实现ado.net 获取DataTable。

编写代码如下:

using System;
using System.Data;
using System.Data.SqlClient;

namespace adonetdemo
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Hello World!");
            string connectionString = "Data Source=.;database=Note;uid=sa;pwd=xxx;";
            SqlConnection conn = new SqlConnection(connectionString);
            conn.Open();
            SqlDataAdapter adapter = new SqlDataAdapter("select * from Notes",conn);
            DataSet dataset = new DataSet();
            adapter.Fill(dataset);
            DataTable dt = dataset.Tables[0];
            foreach (var item in dt.Rows)
            {
                DataRow row=item as DataRow;
                System.Console.WriteLine(row["Title"]);
            }
            System.Console.WriteLine("本文原创LineZero");
        }
    }
}

运行显示结果如下:

 

MVC

新建项目 dotnet new mvc

Program.cs 已经精简如下:

    public class Program
    {
        public static void Main(string[] args)
        {
            BuildWebHost(args).Run();
        }

        public static IWebHost BuildWebHost(string[] args) =>
            WebHost.CreateDefaultBuilder(args)
                .UseStartup<Startup>()
                .Build();
    }

使用dotnet run 运行。

razor

新建项目 dotnet new razor

目录下会发现只有Pages 文件夹,然后只有视图页和视图对应的cs文件,代码也可以在视图页上编写。

将Index改成如下:

<h3>@Model.Name</h3>
Index.cshtml.cs 如下:
    public class IndexModel : PageModel
    {
        public string Name;
        public void OnGet()
        {
            Name="LineZero Blog";
        }
    }

运行程序dotnet run:

 

 

开发调试项目

本文使用VS Code ,版本: 1.14.2 C#插件版本:1.12.0

VS Code 首次使用的话需要确保C#插件全部下载完成如下图:

最新版本VS 2017 15.3 应该也是支持.NET Core 2.0。

选择Yes。

本部分使用adonetdemo 项目做演示。直接调试可以参考之前文章:使用VS Code从零开始开发调试.NET Core 1.1

使用附加调试进行调试。

选择.NET Core Attach 这里在代码中加入Console.ReadKey();,使程序暂时不退出,然后附加。

点击调试,注意选择第一个,dotnet exec 。

然后按任意键,开始进行调试

可以正常调试,并显示信息。

最后.NET Core 2.0 的正式版微软正式发布还有一段时间,本文不代表最终.NET Core 2.0正式版。

 

如果你觉得本文对你有帮助,请点击“推荐”,谢谢。 

posted @ 2017-08-11 09:47  LineZero  阅读(6956)  评论(31编辑  收藏  举报