4

我正在尝试将 NancyFX (clint-eastwood)dotnetcore1.1dotnet-cli 1.0.0-rc4-004771. 我目前的项目结构是 -

CustomBootstrapper.cs
HomeModule.cs
index.sshtml
nancyapp.csproj
Program.cs
Startup.cs

代码是 -

nancyapp.csproj

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

程序.cs

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

启动.cs

using Microsoft.AspNetCore.Builder;
using Nancy.Owin;

namespace nancyapp
{
    public class Startup
    {
        public void Configure(IApplicationBuilder app)
        {
            app.UseOwin(x => x.UseNancy());
        }
    }
}

HomeModule.cs

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

索引.sshtml

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

我不确定是什么导致了错误或如何启用跟踪。任何人都可以帮忙吗?

4

2 回答 2

7

这里有两个问题:

  • 500 是因为没有找到视图,你需要做的是通过实现IRootPathProvider并返回来提供根路径Directory.GetCurrent()
  • 其次,要启用跟踪,您需要public override void Configure(INancyEnvironment environment)添加密钥,因此您会遇到异常。
于 2017-02-17T04:53:12.477 回答
0

在.NET Core 3.1应用程序中结合使用 Nancy和Owin >= v3.

  • 我已经通过从 v3.x降级 Microsoft.AspNetCore.Owin到 v2.2.0解决了这个问题

降级后我的运行设置如下:

设置

也可以返回一个简单的文本进行测试:

Get("/", _ => { return new TextResponse(HttpStatusCode.OK, "Hello world!"); });
于 2020-02-05T08:07:21.973 回答