我可以使用Microsoft Dotnet CLI构建ASP.NET Core Web应用程序吗?

Ban*_*yan 3 c# .net-core asp.net-core asp.net-core-1.0 dotnet-cli

我浏览了一些网站,发现.NET Core正在使用.NET CLI(.NET命令行界面).我使用.NET CLI创建了一个控制台应用程序,它运行正常

但是当我使用Yeoman创建一个ASP.NET Core Web应用程序并运行命令"dotnet restore"时,我收到以下错误:

Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01与DNXCore不兼容,版本= v5.0.

我也试过了

dotnet restore -s https://api.nuget.org/v3/index.json

但得到同样的错误.

如果我运行"dnu restore"然后它运行正常所以我的问题是我可以使用.NET CLI进行Web应用程序,还是仅限于控制台应用程序?

我已经通过了链接

https://github.com/dotnet/cli&http://dotnet.github.io/getting-started/

但是没有找到它支持ASP.NET Core web app的任何细节?

Tse*_*eng 7

我花了一个小时的时间来玩它,我得到了"欢迎页面".

正如我所怀疑的那样,你试图将dotnet-cli与你的dnx风格的项目一起使用,你使用了错误的nuget feed(官方的,而不是每晚的myget feed).

对于此演示项目,只需创建一个新文件夹并运行即可dotnet new.这将创建三个文件:NuGet.Config,project.jsonProgram.cs.您可以删除后一个,只需Startup.cs从下面创建一个.

Startup.cs:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.DependencyInjection;

namespace AspNetCoreCliDemo
{
    public class Startup
    {
        // This method gets called by the runtime. Use this method to add services to the container.
        // For more information on how to configure your application, visit http://go.microsoft.com/fwlink/?LinkID=398940
        public void ConfigureServices(IServiceCollection services)
        {
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app)
        {
            app.UseIISPlatformHandler();
            app.UseWelcomePage();
        }

        // This doesn't work right now, as it can't resolve WebApplication type
        //public static void Main(string[] args) => WebApplication.Run<Startup>(args);

        // Entry point for the application.
        public static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                        .UseDefaultConfiguration(args)
                        .UseServer("Microsoft.AspNetCore.Server.Kestrel")
                        .UseIISPlatformHandlerUrl()
                        .UseStartup<Startup>()
                        .Build();

            host.Run();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

project.json:

{
  "version": "1.0.0-*",
  "compilationOptions": {
    "emitEntryPoint": true
  },

  "dependencies": {
    "NETStandard.Library": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.Diagnostics": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.IISPlatformHandler": "1.0.0-rc2-*",
    "Microsoft.AspNetCore.Server.Kestrel": "1.0.0-rc2-*"
  },

  "frameworks": {
    "dnxcore50": { }
  },

  "exclude": [
    "wwwroot",
    "node_modules"
  ],
  "publishExclude": [
    "**.user",
    "**.vspscc"
  ]
}
Run Code Online (Sandbox Code Playgroud)

注意:dnxcore截至目前仅支持名字对象.

最后但并非最不重要的是,NuGet.Config这在我的案例中起作用:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  <packageSources>
    <!--To inherit the global NuGet package sources remove the <clear/> line below -->
    <clear />
    <add key="cli-deps" value="https://dotnet.myget.org/F/cli-deps/api/v3/index.json" />
    <add key="dotnet-core" value="https://dotnet.myget.org/F/dotnet-core/api/v3/index.json" />
    <add key="api.nuget.org" value="https://api.nuget.org/v3/index.json" />
  </packageSources>
</configuration>
Run Code Online (Sandbox Code Playgroud)

我没有使用两个默认提要(api.nuget.org和dotnet-core)取得成功,因为它无法解决一些依赖关系.添加"cli-deps"Feed后,所有包都得到了解决和dotnet run处理.它将侦听" http:// localhost:5000 "端口并提供欢迎页面.

您可能会收到一条错误消息大约有多个入口点,那是因为你有Main两个方法Program.csStartup.cs.只需删除即可Program.cs.

这应该作为切入点.

dotnet-cli到目前为止还不支持命令(以前在project.json文件中定义的命令).