How to work RavenDB Id with ASP.NET MVC Routes

By default RavenDB's Id would be sperated by "/". Let's say that we have a category object, the Ids would be like "categories/1". This will make problems when working with ASP.NET MVC's route rule. For a route category/edit/id, the uri would be category/edit/categories/1. You can solve this problem in two ways

Solution 1 - Change Id Separator

We can use different Id Separator for RavenDB Ids in order to working with ASP.NET MVC route rules. The following code specify that Ids would be seperated by "-" rather than the default "/"

 documentStore = new DocumentStore { Url = "http://localhost:8080/" };

 documentStore.Initialize();

 documentStore.Conventions.IdentityPartsSeparator = "-";


The above IdentityPartsSeparator would be generate Ids like "categories-1"

Solution 2 - Modify ASP.NET MVC Route

Modify the ASP.NET MVC routes in the Global.asax.cs file, as shown in the following code

 

routes.MapRoute(

    "WithParam",                                           // Route name

    "{controller}/{action}/{*id}"                         // URL with parameters

    );

 We just put "*" in front of the id variable that will be working with the default Id separator of RavenDB

Published Friday, June 4, 2010 1:59 AM by shiju

Comments

# re: How to work RavenDB Id with ASP.NET MVC Routes

Tuesday, September 7, 2010 5:40 PM by Rob

I wanted to use Guids as IDs so I did this in the CategoryController:

[HttpGet]

public ActionResult Create()

{

var category = new Category();

category.Id = Guid.NewGuid().ToString();

return View("Update", category);

}

My MVC urls end up like this:

/Category/Edit/1c2870f3-c4fc-41be-bdcf-96600b3b64fa

Leave a Comment

(required) 
(required) 
(optional)
(required)