天天看点

【dotnet跨平台】最新版dotnet-cli下的ASP.NET Core和asp.net mvc【RC2尝鲜】【dotnet跨平台】最新版dotnet-cli下的ASP.NET Core和asp.net mvc【RC2尝鲜】

RC1是http://get.asp.net里面提供的https://docs.asp.net/en/latest/getting-started/index.html,使用旧的ASP.NET Core 1.0。这个版本会使用dnvm,dnu restore和dnx web等,而RC2则不需要,RC2只需要dotnet restore和dotnet run,无论是对控制台程序还是web程序。

RC2就是在RC1的基础上从ASP.NET Core 1.0迁移到基于dotnet-cli或者.NET Core CLI的跨平台产品。

声明下,RC2还没出来,这里使用的是正在开发的RC2版本的最新版本。

想试下最新版dotnet-cli下的ASP.NET Core和asp.net mvc,可以参考http://dotnet.github.io/getting-started/和https://github.com/aspnet/cli-samples。

【注:】旧方法是使用dnx和mono:https://docs.asp.net/en/latest/getting-started/installing-on-linux.html

【方法一,这个方法尝试失败了,请不要再次尝试】

具体在Linux端运行如下命令(本人使用的是Ubuntu Server 14.04 x64,下载地址:http://www.ubuntu.com/download/server):

sh -c 'echo "deb [arch=amd64] http://apt-mo.trafficmanager.net/repos/dotnet/ trusty main" > /etc/apt/sources.list.d/dotnetdev.list'

apt-key adv --keyserver apt-mo.trafficmanager.net --recv-keys 417A0893

apt-get update

apt-get install dotnet=1.0.0.001675-1

想看最新版dotnet是多少看这个:http://apt-mo.trafficmanager.net/repos/dotnet/pool/main/d/dotnet/, 我这里安装的是最新版1.0.0.001675-1版本。

cd /var

mkdir aspnet

cd /var/aspnet

cd aspnet

git clone https://github.com/aspnet/cli-samples.git

cd cli-samples

dotnet restore

安装完之后,发现有个bug"unknown keyword platform":https://github.com/aspnet/cli-samples/issues/32,官网的步骤不再适用当前cli-samples,解决方法是到这里下载最新版:https://github.com/dotnet/cli#installers-and-binaries

【方法二,这个方法成功,大家可以试试】

https://github.com/dotnet/cli#installers-and-binaries或者https://github.com/dotnet/cli显示了最新版本是1.0.0-beta-002202版本

3个deb都下载下来:

wget http://dotnetcli.blob.core.windows.net/dotnet/beta/Installers/Latest/dotnet-host-ubuntu-x64.latest.deb

wget http://dotnetcli.blob.core.windows.net/dotnet/beta/Installers/Latest/dotnet-sharedframework-ubuntu-x64.latest.deb

wget http://dotnetcli.blob.core.windows.net/dotnet/beta/Installers/Latest/dotnet-sdk-ubuntu-x64.latest.deb

先删除之前安装的旧版的:apt-get remove dotnet=1.0.0.001675-1

dpkg -i dotnet-host-ubuntu-x64.latest.deb

dpkg -i dotnet-sharedframework-ubuntu-x64.latest.deb

dpkg -i dotnet-sdk-ubuntu-x64.latest.deb

安装完成之后,开始尝试下能不能用了:

cd /var/aspnet/cli-samples

cd HelloMvc

dotnet run

http://localhost:5000

如何改变监听IP地址和端口?在这里找到了答案:https://github.com/aspnet/KestrelHttpServer/issues/639

把Program.cs加一行代码如下:

using System.IO;

using Microsoft.AspNetCore.Hosting;

using Microsoft.AspNetCore.Builder;

namespace HelloMvc

{

    public class Program

    {

        public static void Main(string[] args)

        {

            var host = new WebHostBuilder()

                        .UseKestrel()

                        .UseContentRoot(Directory.GetCurrentDirectory())

                        .UseDefaultHostingConfiguration(args)

                        .UseIIS()

                        .UseStartup<Startup>()

                        .UseUrls("http://*:8888")

                        .Build();

            host.Run();

        }

    }

}

运行dotnet run效果如下:

【dotnet跨平台】最新版dotnet-cli下的ASP.NET Core和asp.net mvc【RC2尝鲜】【dotnet跨平台】最新版dotnet-cli下的ASP.NET Core和asp.net mvc【RC2尝鲜】

继续阅读