【发布时间】:2012-07-01 09:36:55
【问题描述】:
我正在关注 Tekpub 上的 RavenDB,讨论如何将它与 ASP.NET MVC 一起使用。我在本地机器上运行 RavenServer.exe 程序,并且我有一个基本控制器设置如下:
public class RavenController : Controller
{
public new IDocumentSession Session { get; set; }
private static IDocumentStore documentStore;
protected override JsonResult Json(object data, string contentType, System.Text.Encoding contentEncoding, JsonRequestBehavior behavior)
{
return base.Json(data, contentType, contentEncoding, JsonRequestBehavior.AllowGet);
}
public static IDocumentStore DocumentStore
{
get
{
if (documentStore != null)
return documentStore;
lock (typeof(RavenController))
{
if (documentStore != null)
return documentStore;
documentStore = new DocumentStore
{
Url = "http://localhost:8080"
}.Initialize();
}
return documentStore;
}
}
protected override void OnActionExecuting(ActionExecutingContext filterContext)
{
Session = DocumentStore.OpenSession();
}
protected override void OnActionExecuted(ActionExecutedContext filterContext)
{
using (Session)
{
if (Session != null && filterContext.Exception == null)
Session.SaveChanges();
}
}
}
我有一个使用 RavenDB 提供的示例专辑数据定义的简单模型(也来自视频):
公开课相册 { 公共字符串 AlbumArtUrl { 获取;放; } 公共字符串标题 { 获取;放; } 公共 int CountSold { 获取;放; } 公共小数价格 { 得到;放; }
public ArtistReference Artist { get; set; }
public GenreReference Genre { get; set; }
}
public class ArtistReference
{
public string Id { get; set; }
public string Name { get; set; }
}
public class GenreReference
{
public string Id { get; set; }
public string Name { get; set; }
}
最后,这是我的控制器:
公共类 HomeController : RavenController { 公共 ActionResult 专辑(字符串 id) { var 专辑 = Session.Load(id); 返回视图(专辑); }
}
现在,当我转到 URL localhost:xxx/home/album/661 时,我根本没有得到任何结果;调试显示“专辑”为空,因此 RavenDB 没有加载任何内容。查看服务器,我看到以下内容,它收到 404 请求路径 /docs/661。但是,当我使用 RavenDb 工作室访问相关专辑时,它查找的 URL(返回数据)是 /docs/albums/661。因此,当 RavenDB 可以通过管理工作室正确找到它们时,似乎我在某个地方遗漏了一些东西,让 RavenDB 能够通过 MVC 请求找到文档。
有什么我忘记的想法吗?
【问题讨论】:
标签: asp.net-mvc ravendb