Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 773
RPM1984 Avatar asked Sep 09 '11 11:09

RPM1984


3 Answers

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

like image 69
Ayende Rahien Avatar answered Nov 17 '22 11:11

Ayende Rahien


You can simply use ...

int Id;

..instead of ...

string Id;

in your entity classes :)

like image 30
Korayem Avatar answered Nov 17 '22 10:11

Korayem


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.

like image 40
Daniel Lang Avatar answered Nov 17 '22 11:11

Daniel Lang