【问题标题】:RavenDB with MVC looking for wrong path and 404带有 MVC 的 RavenDB 寻找错误的路径和 404
【发布时间】: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


    【解决方案1】:

    韦恩, 你的问题就在这里:

    public ActionResult Album(string id)
    

    您使用的是字符串 id,但您只传递了数字部分,RavenDB 认为您正在为其提供完整的 id,并尝试加载 id 为“661”的文档

    相反,您可以这样定义:

    public ActionResult Album(int id)
    

    RavenDB 知道您正在向它传递一个值类型,并且约定提供其余的 id。

    【讨论】:

    • 啊,太好了,谢谢 Ayende!我想这可能在某个时候发生了变化,因为在视频中你使用 string id 就好了。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-09-21
    • 2018-09-27
    • 2020-04-23
    • 1970-01-01
    相关资源
    最近更新 更多