首页 新闻 搜索 专区 学院

asp.net core程序运行出错:"A valid non-empty application name must be provided."

0
悬赏园豆:30 [已解决问题] 解决于 2018-02-01 16:17

用 dotnet run 命令运行一个 asp.net core 站点出现如下的错误:

Unhandled Exception: System.ArgumentException: A valid non-empty application name must be provided.
Parameter name: applicationName
   at Microsoft.AspNetCore.Hosting.Internal.HostingEnvironmentExtensions.Initialize(IHostingEnvironment hostingEnvironment, String applicationName, String contentRootPath, WebHostOptions options)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.BuildCommonServices(AggregateException& hostingStartupErrors)
   at Microsoft.AspNetCore.Hosting.WebHostBuilder.Build()
   at WebApi.Program.BuildWebHost(String[] args) in D:\dev\WebApi\Program.cs:line 18
   at WebApi.Program.<Main>d__0.MoveNext() in D:\dev\WebApi\Program.cs:line 14
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at WebApi.Program.<Main>(String[] args)

Program.cs 中的代码如下:

public class Program
{
    public static async Task Main(string[] args)
    {
        await BuildWebHost(args).RunAsync();
    }

    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .Build();
}
dudu的主页 dudu | 高人七级 | 园豆:38832
提问于:2018-02-01 16:05
< >
分享
最佳答案
0

是由于少了.UseStartup<Startup>(),而且 Startup 中至少包含 Configure 方法

public class Startup
{
    public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
    }
}
dudu | 高人七级 |园豆:38832 | 2018-02-01 16:17

如果不用 Startup ,可以使用 Configure(_ => {}) 避免出现这个异常

public static IWebHost BuildWebHost(string[] args) =>
    WebHost.CreateDefaultBuilder(args)
        .Configure(_ => {})
        .Build();
dudu | 园豆:38832 (高人七级) | 2018-02-13 20:42
其他回答(1)
1

是这样吗:
    public static IWebHost BuildWebHost(string[] args) =>
        WebHost.CreateDefaultBuilder(args)
            .UseStartup<Startup>()
            .Build();

ll...ll | 园豆:364 (菜鸟二级) | 2018-02-01 16:22

是的

支持(0) 反对(0) dudu | 园豆:38832 (高人七级) | 2018-02-01 16:23
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册