c# - 我可以自己调用一个 nancy 模块吗

标签 c# .net nancy

假设我有这个 Nancy 模块

public class ProductModule : NancyModule
{
    public ProductModule(IProductRepository repo)
    {
        Get["/products/list"] = _ =>
        {
            ViewBag.Categories = repo.GetAllCategories();
            return repo.GetAllProducts();
        };
    }
}

我将 Razor 用作 ViewEngine,并且能够显示产品列表。

现在我希望能够在 Windows 窗体应用程序中执行相同的请求。我知道我可以做类似的事情

var bootstrapper = new CustomBootstrapper();
bootstrapper.Initialise();
var engine = bootstrapper.GetEngine();
var request = new Request("GET", "/products/list.xml", "http");
var context = engine.HandleRequest(request);

无论如何,这不符合我的需要,因为它涉及整个 http 管道并序列化我的 IQueryable .但是在我的 Windows 窗体应用程序中,我已经有了对 IQueryable<> 的适当分页支持。对象。目前我应该有一些冗余代码

public class ProductController
{
    dynamic ViewBag = new ExpandoObject();
    public dynamic List()
    {
        ViewBag.Categories = repo.GetAllCategories();
        return repo.GetAllProducts();
    }
}

我想摆脱它,只使用我的 nancy 模块。

基本上这必须以某种方式成为可能,因为在我的 razor view 中我拥有我想要的东西并且可以完全访问 ViewBag 和 Model。

我已经下载了源代码,但还没有找到正确的按钮来按下。

如有任何建议,我们将不胜感激。

最佳答案

虽然我不确定为什么 @eth0 的建议不适合您的特定情况,但 Nancy.Testing 至少提供了一种绕过网络堆栈的方法。

var bootstrapper = new CustomBootstrapper();
var browser = new Browser(bootstrapper);
var result = browser.Get("/products/list.xml", with => { with.HttpRequest(); });

更多细节参见官方文档:Testing your application

关于c# - 我可以自己调用一个 nancy 模块吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24777252/

相关文章:

c# - 如何将图片保存到数据库中?

c# - GC 会停止 .NET 中的所有应用程序线程吗?

c# - 实例化要在类库中使用的委托(delegate)方法

url-routing - NancyFX Catch All 路线

NancyFx 未使用 Owin Hosting 到达模块

c# - 在 Windows 环境中使用 Etsy 的 StatsD

c# - 当服务具有其他依赖项时,无法让注入(inject)在 Azure Functions 中工作

c# - 为什么 Convert.ToDateTime() 在此示例中不起作用?

javascript - .NET 压缩中是否可以不进行修改?

nancy - 如何在 Nancy 中提供静态内容