Nancy Self Host 带有 Razor 视图的空白响应

发布于 2024-11-23 15:03:35 字数 1032 浏览 1 评论 0原文

在 Nancy 0.6 中已解决


我试图让自托管 Nancy 返回剃刀视图,但无法让它工作。 Nancy 源代码中的示例使用 Web 项目,但他们拥有页面 并没有说这是必需的。我尝试指定配置部分,但他们再次说“此步骤完全可选”(斜体是他们的)。追踪源代码,它看起来不像 razor 是一个有效的视图引擎,但我看不到可以在配置或我自己的 NancyModule 中添加它的位置......任何帮助将不胜感激。

View Engines

当我最终发现他们正在查看视图文件夹时,似乎 cshtml 是受支持的扩展,但是DefaultViewFactory 没有与视图引擎关联,因此我得到 null:

在此处输入图像描述

我的代码:

public Module1()
{
    Get["/me"] = parms =>
    {
        return View["Static.html"]; // WORKS!
    };
    Get["/you"] = parms =>
    {
        dynamic model = new ExpandoObject();
        //return View["~/Static.cshtml", model];
        //return View["/Static.cshtml", model];
        return View["Static.cshtml", model]; // blank page, no error or anything
    };
}

Static.cshtml 只是一个 html 页面,上面写着“Hello, world!”

Resolved in Nancy 0.6


I'm trying to get self-hosted Nancy to return a razor view and I can't get it to work. The sample in the Nancy source code uses a web project, but the page they have doesn't say this is required. I've tried specifying the config sections but again they say "This step is totally optional" (italics are theirs). Tracing through the source it doesn't look like razor is a valid view engine, but I don't see where I can add it either in the config or in my own NancyModule... Any help would be appreciated.

View Engines

When I finally figured out they were looking in the views folder, it seems that the cshtml is a supported extension, but the DefaultViewFactory doesn't have it associated with a view engine so I get null:

enter image description here

My code:

public Module1()
{
    Get["/me"] = parms =>
    {
        return View["Static.html"]; // WORKS!
    };
    Get["/you"] = parms =>
    {
        dynamic model = new ExpandoObject();
        //return View["~/Static.cshtml", model];
        //return View["/Static.cshtml", model];
        return View["Static.cshtml", model]; // blank page, no error or anything
    };
}

Static.cshtml is just an html page that says "Hello, world!"

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

多情出卖 2024-11-30 15:03:35

控制台、表单和 wpf 项目将视图文件定位在与可执行文件相同的位置。这意味着您必须将 view.cshtml 文件复制到项目 bin\debug 文件夹才能在调试模式下工作。
因此:将您的 .cshtml 文件标记为复制到输出

console,form and wpf projects locate view files at same location as executable file. it means you must copy your view.cshtml files to your projects bin\debug folder to work on debug mode.
So: mark your .cshtml files as copy to output

独夜无伴 2024-11-30 15:03:35

我解决了问题的一部分,当我创建 NancyHost 时,剃刀程序集没有加载到我的 AppDomain 中。 NancyHost 具有 TinyIoc 扫描功能,并在启动时创建所有已加载程序集中所有类的列表,并且该列表永远不会更新。我通过创建 RazorViewEngine 来强制加载程序集来修复此问题。使用 Register() 调用也可以,但我认为只是因为它强制加载程序集,我认为 Nancy 有自己的容器。所有这些位置都有效,但如果我将其放入 NancyModule 中,它仍然不起作用:

//TinyIoC.TinyIoCContainer.Current.Register<RazorViewEngine>(); // WORKS
//RazorViewEngine rve = new RazorViewEngine(); // WORKS
m_Host = new NancyHost(m_Uri);
//TinyIoC.TinyIoCContainer.Current.Register<RazorViewEngine>(); // WORKS
m_Host.Start();
TinyIoC.TinyIoCContainer.Current.Register<RazorViewEngine>(); // WORKS

如果有人想重写此答案并找出更清晰的方法或更好的解决方案,我会接受答案。

I figured out one part of my problem, the razor assembly wasn't loaded into my AppDomain when I created my NancyHost. NancyHost has TinyIoc scan and create a list of all classes in all loaded assemblies when it starts, and this list never gets udpated. I fixed it by creating a RazorViewEngine to force the assembly to load. Using the Register() call works also, but I think only because it forces the assembly to load, I think Nancy has their own container. All of these locations works, but it still does not work if I put it in my NancyModule:

//TinyIoC.TinyIoCContainer.Current.Register<RazorViewEngine>(); // WORKS
//RazorViewEngine rve = new RazorViewEngine(); // WORKS
m_Host = new NancyHost(m_Uri);
//TinyIoC.TinyIoCContainer.Current.Register<RazorViewEngine>(); // WORKS
m_Host.Start();
TinyIoC.TinyIoCContainer.Current.Register<RazorViewEngine>(); // WORKS

If someone wants to re-write this answer and figure out a cleaner way or a better solution, I'll accept the answer.

心碎的声音 2024-11-30 15:03:35

我在 nancy 0.8.0 和 Razor 上也遇到了类似的问题。我下载并安装了 Nancy、Host.Self 和 ViewEngine.Razor,并进行了引用。

尽管如此,在编译时,编译器无法在 Nancy.ViewEngines.Razor 中找到 Razor 命名空间(尽管对象浏览器和 Reflector 很好地显示了命名空间和类型),我还是执行了所有正常的命名空间引用 vodo;清理、删除、添加、pm卸载、pm安装、手动将dll移至垃圾箱、戴上我幸运的编译帽、向我的左肩吐口水三遍等等,但仍然得到相同的结果。没有将 Razor 加载到 AppDomain 中...

我恢复为手动将程序集加载到 appdomain 中,并使用 activator 为 Tiny.IoC 容器工厂创建 RazorViewEngine 类型的实例。

实际问题是我的解决方案的启动项目是针对 .NET Framework 4 客户端配置文件的。显然,客户端 .Net 包无法使用和查看为完整 4 版本编译的 dll 的某些部分。

无论如何。将我的构建更改为“正常”.NET 4,一切都恢复正常。编译器在命名空间中找到类型,Tiny.IoC 能够将所有内容连接起来。

I had a sort of similar problem with nancy 0.8.0 and Razor. I had Nancy, Host.Self and ViewEngine.Razor downloaded and installed, and referenced.

Still when compiling the compiler could not find the Razor namespace in Nancy.ViewEngines.Razor (event though the object browser and Reflector showed that the namespace and the types perfectly well), I did all the normal namespace reference vodo; clean, remove, add, pm unstall, pm install, manually moved the dlls to the bin, put on my lucky compiling hat, spat three times over my left shoulder etcetera but still got the same result. No Razor loaded into the AppDomain...

I reverted to manually loading the assembly into the appdomain and use activator to create a instance of the RazorViewEngine type for the Tiny.IoC Container factory.

The actual problem was that the startup project of my solution was targeted to the .NET Framework 4 client Profile. Apparently the client .Net bundle is unable to consume and view some parts for a dll compiled for the full 4 version.

Anyways. Changed my build to "normal" .NET 4 and everything is back to normal. The compiler finds the types in the namespace and Tiny.IoC is able to hook everything up.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文