gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 13:36:19 24 4
gpt4 key购买 nike

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

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

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 应用程序的任何详细信息?

最佳答案

我花了一个小时来摆弄它,我得到了用它运行的“欢迎页面”。

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

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

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

项目.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 名字对象。

最后但同样重要的是,在我的案例中有效的 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>

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

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

这应该作为一个入口点。

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

关于c# - 我可以使用 Microsoft Dotnet CLI 构建 ASP.NET Core Web 应用程序吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35527151/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com