RavenDb with ASP.NET MVC 3 - How to generate URL with ID?

三世轮回 提交于 2019-12-10 01:45:25

问题


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?


回答1:


RPM1984, There are several ways you can deal with that.

1) You can modify your routing to handle this:

routes.MapRoute(
    "Default",                                                // Route name
    "{controller}/{action}/{*id}",                            // URL with parameters
    new { controller = "Home", action = "Index", id = "" });  // Parameter defaults

This will allow MVC to accept parameters with slashes in them

2) You can modify the default id generation strategy:

 documentStore.Conventions.IdentityPartsSeparator = "-";

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




回答2:


You can simply use ...

int Id;

..instead of ...

string Id;

in your entity classes :)




回答3:


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.



来源:https://stackoverflow.com/questions/7360849/ravendb-with-asp-net-mvc-3-how-to-generate-url-with-id

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!