简体   繁体   English

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

[英]Can I build ASP.NET Core web application using Microsoft Dotnet CLI?

I have gone through some websites and found that .NET Core is using .NET CLI (.NET Command Line Interface). 我浏览了一些网站,发现.NET Core正在使用.NET CLI (.NET命令行界面)。 I have create a console application using .NET CLI and it is working fine 我使用.NET CLI创建了一个控制台应用程序,它运行正常

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: 但是当我使用Yeoman创建一个ASP.NET Core Web应用程序并运行命令“dotnet restore”时,我收到以下错误:

Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 is not compatible with DNXCore,Version=v5.0. Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01与DNXCore不兼容,版本= v5.0。

I also tried 我也试过了

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

But getting the same error. 但得到同样的错误。

If I run “dnu restore” then it is working fine so my question is can I use .NET CLI for web application too or it is only limited to console application? 如果我运行“dnu restore”然后它运行正常所以我的问题是我可以使用.NET CLI进行Web应用程序,还是仅限于控制台应用程序?

I have gone through the links 我已经通过了链接

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

but did not find any details that it supports ASP.NET Core web app or not? 但是没有找到它支持ASP.NET Core web app的任何细节?

I've spent a good hour on toying around with it and I got the "Welcome page" running with it. 我花了一个小时的时间来玩它,我得到了“欢迎页面”。

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-cli与你的dnx风格的项目一起使用,你使用了错误的nuget feed(官方的,而不是每晚的myget feed)。

For this demo project, just create a new folder and run dotnet new . 对于此演示项目,只需创建一个新文件夹并运行dotnet new This creates three files: NuGet.Config , project.json and Program.cs . 这将创建三个文件: NuGet.Configproject.jsonProgram.cs You can delete the later one and just create a Startup.cs from below. 您可以删除后一个,只需从下面创建一个Startup.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();
        }
    }
}

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

Note: Only dnxcore moniker is supported as of now. 注意:截至目前仅支持dnxcore名字对象。

Last but not least, the NuGet.Config that worked in my case: 最后但并非最NuGet.Config是,在我的案例中工作的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>

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. 我没有使用两个默认提要(api.nuget.org和dotnet-core)取得成功,因为它无法解决一些依赖关系。 After adding the "cli-deps" feed, all packages were resolved and dotnet run worked. 添加“cli-deps”订阅源后,所有包都已解析并且dotnet run It will listen on the " http://localhost:5000 " port and serve the welcome page. 它将侦听“ http:// localhost:5000 ”端口并提供欢迎页面。

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 . 您可能会收到有关多个入口点的错误消息,这是因为您在Program.csStartup.cs中都有一个Main方法。 Just delete the Program.cs . 只需删除Program.cs

This should serve as an entry point. 这应该作为切入点。

dotnet-cli as of now doesn't support commands yet (ones formerly defined in the project.json file). 截至目前, dotnet-cli还不支持命令(以前在project.json文件中定义的命令)。

Short Answer 简答

For ASP.NET Core rc1, we use dnx and dnu . 对于ASP.NET Core rc1,我们使用dnxdnu

For ASP.NET Core rc2, we use dotnet. 对于ASP.NET Core rc2,我们使用dotnet.

Longer Answer 更长的答案

I have gone through some websites and found that .NET Core is using .NET CLI (.NET Command Line Interface). 我浏览了一些网站,发现.NET Core正在使用.NET CLI(.NET命令行界面)。 I have create a console application using .NET CLI and it is working fine. 我使用.NET CLI创建了一个控制台应用程序,它运行正常。

ASP.NET Core rc2 is also a console application. ASP.NET Core rc2也是一个控制台应用程序。 That is, it is a console application that starts the ASP.NET hosting layer. 也就是说,它是一个启动ASP.NET托管层的控制台应用程序。

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... 但是当我使用Yeoman创建一个ASP.NET Core Web应用程序并运行命令“dotnet restore”时,我收到以下错误...

My sense is that you are running these commands: 我的感觉是你正在运行这些命令:

yo aspnet
? What type of application do you want to create? Web Application
? What's the name of your ASP.NET application? MyApp

This creates an ASP.NET Core rc1 project. 这将创建一个ASP.NET Core rc1项目。 You can see this by looking at the resultant project.json file dependencies. 您可以通过查看生成的project.json文件依赖项来查看此信息。 They all say rc1-final . 他们都说rc1-final Result: you cannot build these with the dotnet command line. 结果:您无法使用dotnet命令行构建它们。 You still have to use the dnu build and the dnx <command> formulas. 您仍然必须使用dnu builddnx <command>公式。

...can I use .NET CLI for web application[s] too or it is only limited to console application? ...我可以将.NET CLI用于Web应用程序[s],还是仅限于控制台应用程序?

In ASP.NET Core rc1, the dnx.exe made a call to new WebHostBuilder() , so we need to use dnx to start our web application. 在ASP.NET Core rc1中, dnx.exe调用了new WebHostBuilder() ,因此我们需要使用dnx来启动我们的Web应用程序。 In ASP.NET Core rc2, the console application that contains our web app makes its own call to new WebHostBuilder() , so we can use the dotnet command line interface to start the app. 在ASP.NET Core rc2中,包含我们的Web应用程序的控制台应用程序自己调用new WebHostBuilder() ,因此我们可以使用dotnet命令行界面来启动应用程序。 So... 所以...

  • No you cannot use the dotnet CLI for ASP.NET Core rc1 Web Applications, because dnx.exe loads the ASP.NET Hosting Layer. 不,您不能将dotnet CLI用于ASP.NET Core rc1 Web应用程序,因为dnx.exe会加载ASP.NET主机层。
  • Yes you can use it for ASP.NET Core rc2 Web Applications, because your console application loads the ASP.NET Hosting Layer itself. 是的,您可以将它用于ASP.NET Core rc2 Web应用程序,因为您的控制台应用程序会加载ASP.NET主机层本身。

Now I don't know what your exact error is, but this might be helpful: 现在我不知道你的确切错误是什么,但这可能会有所帮助:

Basically, you need to add the appropriate imports to your project.json: 基本上,您需要将适当的导入添加到project.json:

    "frameworks": {
        "dnxcore50": { 
            "imports": "portable-net451+win8"
        }
    }

(As posted here: https://github.com/aspnet/Home/issues/1265 ) (发布在这里: https//github.com/aspnet/Home/issues/1265

These were my errors: 这些是我的错误:

Errors in /webserver/project.json
    Package Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 is not compatible with dnxcore50 (DNXCore,Version=v5.0). Package Microsoft.CodeAnalysis.CSharp 1.1.0-rc1-20151109-01 supports:
      - net45 (.NETFramework,Version=v4.5)
      - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7)
    Package Microsoft.CodeAnalysis.Common 1.1.0-rc1-20151109-01 is not compatible with dnxcore50 (DNXCore,Version=v5.0). Package Microsoft.CodeAnalysis.Common 1.1.0-rc1-20151109-01 supports:
      - net45 (.NETFramework,Version=v4.5)
      - portable-net45+win8 (.NETPortable,Version=v0.0,Profile=Profile7)
    One or more packages are incompatible with DNXCore,Version=v5.0.

I generated the .Net Web App using the Swagger Code generator from http://editor.swagger.io/#/ 我使用来自http://editor.swagger.io/#/的Swagger Code生成器生成.Net Web App

After adding the imports, dotnet restore worked without problems. 添加导入后, dotnet restore没有问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 是否可以使用 Roslyn 构建 ASP.Net Core Web 应用程序? - Is that possible to build an ASP.Net Core web application using Roslyn? 如何使用 dotnet CLI 构建 .NET Framework 4.8 应用程序? - How can I build a .NET Framework 4.8 application using the dotnet CLI? 使用c#使用Microsoft.Build.Engine构建和发布(ASP.NET Web应用程序) - Build and Publish (ASP.NET Web Application) using Microsoft.Build.Engine using c# 我可以将 ASP.NET Core MVC 添加到现有的 ASP.NET Web Z645024253191A381C36Z83CAE8 - Can I add ASP.NET Core MVC to existing ASP.NET Web Forms Application 使用 Microsoft 标识为 ASP.NET 核心 web 应用程序检索不记名令牌 - Retrieving a Bearer token using Microsoft Identity for an ASP.NET core web application ASP.NET Core 3.1 Web 应用程序在 IIS Express 上运行时抛出错误 500.30,但在使用 dotnet watch run 时不会抛出错误 - ASP.NET Core 3.1 web application throws error 500.30 when run on IIS Express, but not when using dotnet watch run 无法在 ASP.NET Core 中安装“Microsoft.EntityFrameworkCore.Tools.Dotnet” - Can't install "Microsoft.EntityFrameworkCore.Tools.Dotnet" in ASP.NET Core CLI的ASP.NET Core发布应用程序 - ASP.NET Core publishing application by CLI 无法发布.NET Core Web Application(无法加载文件或程序集Microsoft.DotNet.ProjectModel) - Can't publish .NET Core Web Application (could not load file or assembly Microsoft.DotNet.ProjectModel) 在asp.net核心中的post build事件中运行dotnet发布 - running dotnet publish in post build event in asp.net core
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM