th1*_*ey3 4 nancy .net-core dotnet-cli
我正在尝试使用NancyFX(clint-eastwood)dotnetcore1.1和dotnet-cli 1.0.0-rc4-004771.我目前的项目结构是 - 
CustomBootstrapper.cs
HomeModule.cs
index.sshtml
nancyapp.csproj
Program.cs
Startup.cs
代码是 -
<Project Sdk="Microsoft.NET.Sdk.Web">
  <PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <PackageReference Include="Microsoft.AspNetCore.Owin">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Microsoft.AspNetCore.Server.Kestrel">
      <Version>1.1.0</Version>
    </PackageReference>
    <PackageReference Include="Nancy">
      <Version>2.0.0-clinteastwood</Version>
    </PackageReference>
  </ItemGroup>
</Project>
using System.IO;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
namespace nancyapp
{
    class Program
    {
        static void Main(string[] args)
        {
            var host = new WebHostBuilder()
                .UseKestrel()
                .UseContentRoot(Directory.GetCurrentDirectory())
                .UseStartup<Startup>()
                .Build();
            host.Run();
        }
    }
}
using Microsoft.AspNetCore.Builder;
using Nancy.Owin;
namespace nancyapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseOwin(x => x.UseNancy());
        }
    }
}
using Nancy;
namespace nancyapp
{
    public class HomeModule : NancyModule
    {
        public HomeModule()
        {
            Get("/", _ => { return View["index.sshtml"]; });
            Get("/test/{name}", args => new Person() { Name = args.name });
        }
    }
    public class Person
    {
        public string Name { get; set; }
    }
}
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Document</title>
</head>
<body>
    Welcome to Nancy App.
</body>
</html>
CustomBootstrapper.cs 目前是空的.
当我尝试Get("/test/{name}", args => new Person() { Name = args.name });从休息客户端访问时,我得到了预期的结果.
但是,当我尝试访问root或者Get("/", _ => { return View["index.sshtml"]; });,我收到500服务器错误说 -
错误详细信息目前已禁用.要启用它,请将TraceConfiguration.DisplayErrorTraces设置为true.例如,通过覆盖Bootstrapper的Configure方法并调用environment.Tracing(enabled:false,displayErrorTraces:true)
我尝试按照错误消息中的说明进行操作,并通过包含以下代码启用错误跟踪 CustomBootstrapper.cs
protected override void ApplicationStartup(Nancy.TinyIoc.TinyIoCContainer container, IPipelines pipelines)
{
    var environment = GetEnvironment();
    environment.Tracing(true, true);
}
但是在尝试运行应用程序时出现以下错误 dotnet run
Unhandled Exception: System.ArgumentException: An item with the same
  key has already been added. Key: Nancy.TraceConfiguration at
  System.ThrowHelper.ThrowAddingDuplicateWithKeyArgumentException(Object key) at 
  System.Collections.Generic.Dictionary`2.Insert(TKey key,TValue value, Boolean add) at
  nancyapp.CustomBootstrapper.ApplicationStartup(TinyIoCContainer container, IPipelines pipelines) in D:\TempWork\nancyapp\CustomBootstrapper.cs:line 17 at
  Nancy.Bootstrapper.NancyBootstrapperBase`1.Initialise() at
  Nancy.Owin.NancyMiddleware.UseNancy(NancyOptions options) at
  Nancy.Owin.DelegateExtensions.UseNancy(Action`1 builder, NancyOptionsoptions) at 
  nancyapp.Startup.<>c.<Configure>b__0_0(Action`1 x) in D:\TempWork\nancyapp\Startup.cs:line 10 at
  Microsoft.AspNetCore.Builder.OwinExtensions.UseOwin(IApplicationBuilder builder, Action`1 pipeline) at
  nancyapp.Startup.Configure(IApplicationBuilder app) in D:\TempWork\nancyapp\Startup.cs:line 10
  --- End of stack trace from previous location where exception was thrown --- at
  System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw() at
  Microsoft.AspNetCore.Hosting.ConventionBasedStartup.Configure(IApplicationBuilder app) at
  Microsoft.AspNetCore.Hosting.Internal.WebHost.BuildApplication() at
  Microsoft.AspNetCore.Hosting.WebHostBuilder.Build() at
  nancyapp.Program.Main(String[] args) in D:\TempWork\nancyapp\Program.cs:line 11
我不确定是什么导致错误或如何启用跟踪.有人可以帮忙吗?
小智 6
这里有两个问题:
IRootPathProvider和返回提供根路径Directory.GetCurrent().public override void Configure(INancyEnvironment environment)添加密钥,因此您将获得异常.| 归档时间: | 
 | 
| 查看次数: | 905 次 | 
| 最近记录: |