我可以使用 Microsoft Dotnet CLI 构建 ASP.NET Core Web 应用程序吗? [英] Can I build ASP.NET Core web application using Microsoft Dotnet CLI?

查看:25
本文介绍了我可以使用 Microsoft Dotnet CLI 构建 ASP.NET Core Web 应用程序吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have gone through some websites and found that .NET Core is using .NET CLI (.NET Command Line Interface). I have create a console application using .NET CLI and it is working fine

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

But when I am creating an ASP.NET Core web application using Yeoman and running the command "dotnet restore" then I am getting the following error:

Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 与 DNXCore 不兼容,Version=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 应用程序的任何详细信息?

but did not find any details that it supports ASP.NET Core web app or not?

推荐答案

我已经花了一个小时来玩弄它,我让欢迎页面"运行起来.

I've spent a good hour on toying around with it and I got the "Welcome page" running with it.

正如我怀疑的那样,您尝试将 dotnet-cli 与您的 dnx 样式项目一起使用,并且您使用了错误的 nuget 提要(官方提要,而不是每晚的 myget 提要).

As I suspected, you tried to use dotnet-cli with your dnx styled project and you you used the wrong nuget feed (the official one, rather than the nightly myget feeds).

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

For this demo project, just create a new folder and run dotnet new. This creates three files: NuGet.Config, project.json and Program.cs. You can delete the later one and just create a Startup.cs from below.

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();
        }
    }
}

project.json:

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"
  ]
}

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

Note: Only dnxcore moniker is supported as of now.

最后但并非最不重要的是,在我的案例中有效的 NuGet.Config:

Last but not least, the NuGet.Config that worked in my case:

<?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>

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

I didn't had the success with the two default feeds (api.nuget.org and dotnet-core) as it couldn't resolve a few dependencies. After adding the "cli-deps" feed, all packages were resolved and dotnet run worked. It will listen on the "http://localhost:5000" port and serve the welcome page.

您可能会收到有关具有多个入口点的错误消息,那是因为您在 Program.csStartup.cs 中都有一个 Main 方法.只需删除 Program.cs.

You may get an error message about having more than one entry point, that's because you got a Main method in both Program.cs and Startup.cs. Just delete the Program.cs.

这应该作为一个切入点.

This should serve as an entry point.

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

dotnet-cli as of now doesn't support commands yet (ones formerly defined in the project.json file).

这篇关于我可以使用 Microsoft Dotnet CLI 构建 ASP.NET Core Web 应用程序吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆