我跟随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提供的示例相册数据(也来自视频)定义了一个简单的模型:
public class Album {public string AlbumArtUrl {get;组; } public string Title {get;组; public int CountSold {get;组;公共小数价格{get;组; }
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; }
}
最后,这是我的控制器:
public class HomeController:RavenController {public ActionResult Album(string id){var album = Session.Load(id); return View(专辑); }
}
现在,当我转到URL localhost:xxx/home/album/661
时,我根本没有得到任何结果;调试显示"album"为空,因此RavenDB没有't loading anything. Looking at the server I see the following that it'得到404请求路径 /docs/661
. 但是,当我使用RavenDb工作室访问相关专辑时,它查找的URL(返回数据)是 /docs/albums/661
. 因此,当我们可以通过管理工作室正确找到它们时,似乎我错过了某些地方让RavenDB能够通过MVC请求找到文档 .
我忘了什么想法?
1 回答
WayneM,您的问题在这里:
您正在使用字符串ID,但您只传递数字部分,RavenDB认为您正在为其提供完整ID,并尝试加载ID为“661”的文档
相反,您可以像这样定义它:
然后RavenDB知道你传递了一个值类型,并且约定提供了id的其余部分 .