RavenDb 与 ASP.NET MVC 3 - 如何生成带有 ID 的 URL?
这可能是一个非常简单的答案,但我是 RavenDb 的新手,所以我显然错过了一些东西。
我有一个具有 id 默认约定的基本对象:
public string Id { get; set; }
当我将其保存到文档存储时,我看到它的值如下:
帖子/123
这很好,但是...我如何生成这样的 URL:
www.mysite.com/edit/123
如果我这样做:
@Html.ActionLink("Edit", "Posts", new { id = @Model.Id })
它将生成以下 URL:
www.mysite.com/edit/posts/123
这不是我想要的。
我当然不需要进行字符串操作吗?人们如何看待这个问题?
This is probably a very simple answer, but i'm new to RavenDb, so i'm obviously missing something.
I've got a basic object with the default convention for id:
public string Id { get; set; }
When i save it to the document store, i see it gets a value of like:
posts/123
Which is fine, but...how do i generate a URL like this:
www.mysite.com/edit/123
If i do this:
@Html.ActionLink("Edit", "Posts", new { id = @Model.Id })
It will generate the followiung URL:
www.mysite.com/edit/posts/123
Which is not what i want.
Surely i don't have to do string manipulation? How do people approach this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
转速1984,
有几种方法可以解决这个问题。
1) 您可以修改路由来处理此问题:
这将允许 MVC 接受带有斜杠的参数
2) 您可以修改默认的 id 生成策略:
这将生成以下 id:
posts-1
帖子-2
等
另见此处:
http:// weblogs.asp.net/shijuvarghese/archive/2010/06/04/how-to-work-ravendb-id-with-asp-net-mvc-routes.aspx
RPM1984,
There are several ways you can deal with that.
1) You can modify your routing to handle this:
This will allow MVC to accept parameters with slashes in them
2) You can modify the default id generation strategy:
This will generate ids with:
posts-1
posts-2
etc
See also here:
http://weblogs.asp.net/shijuvarghese/archive/2010/06/04/how-to-work-ravendb-id-with-asp-net-mvc-routes.aspx
使用 ...
.. 而不是 ... :)
您可以简单地在实体类中
You can simply use ...
..instead of ...
in your entity classes :)
实际上,您必须从基于字符串的文档 ID 中提取整数值。这是因为 raven 实际上可以处理任何类型的 Id,不一定是 HILO 生成的整数(如果您没有自己指定 id,则这是默认值)。
看一下 RaccoonBlog 示例。里面有一个帮助器类“RavenIdResolver”,可以很容易地从文档 ID 中获取数字 ID。
Actually you have to extract the integer value out of the documents string-based id. This is because raven can actually handle any kind of Id, not necessarily a HILO-generated integer (this is default if you do not specify an id by your own).
Take a look at RaccoonBlog sample. There is a helper class "RavenIdResolver" inside which makes it really easy to get the numeric id out of the documents-id.