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

2019-04-20 03:18发布

问题:

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.