假设我有这个 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/