RavenDB Ids and ASP.NET MVC3 Routes

后端 未结 3 911
误落风尘
误落风尘 2021-02-13 05:15

Just building a quick, simple site with MVC 3 RC2 and RavenDB to test some things out.

I\'ve been able to make a bunch of projects, but I\'m curious as to how Html.Acti

相关标签:
3条回答
  • 2021-02-13 05:50

    I just downloaded the latest version of RavenDB and tried this out.

    public class Entity {
        public int Id { get;set; }
        public string Text { get;set; }
    }
    

    When I saved it to RavenDB, the id in Raven was "entities/1", but the mapping in RavenDB client was able to successfully interpret the Id from what was in the database to the integer that I wanted.

    var entity = session.Load<Entity>(1);
    Assert.IsTrue(entity.Id == 1);
    

    You do not need a extension method, and this way you would not need to alter any routes as mentioned above, because you will be dealing with good ol' integers. The string Ids were almost a deal breaker, but amazingly this works so RavenDB is back in the hunt.

    • Note: I figured this out from watching a Rob Ashton talk and realizing that all his document classes had integers as Ids.
    0 讨论(0)
  • 2021-02-13 05:58

    I was having a similar issue and came up with my own solution before I found the link to Shiju Varghese blog post referenced by @jfar.

    It might not be as clean and simple as the solutions provided in the blog post, but I do believe it can compete as a solution none the less. So here goes:

    In a standard model class, when using RavenDB, we normally have an id property like so:

    public string Id { get; set; }
    

    What I did, was to add another id property like so:

    public int? IdInt
    {
        get { return int.Parse(Id.Substring(Id.IndexOf("/") + 1)); }
    }
    

    This will give us the number part of the given RavenDB id. I then had a class that looked something like this:

    [Bind(Exclude = "IdInt")]
    public class Item
    {
        public string Id { get; set; }
        public int? IdInt
        {
            get { return int.Parse(Id.Substring(Id.IndexOf("/") + 1)); }
        }
    
        ...
    }
    

    Note that I have excluded the IdInt property, as I don't want the model binder to handle it. Furthermore, note, that the IdInt property is nullable. This is to avoid problems with the model binder later on, when we are creating new items and the Id property is null.

    Then in the routes I declared a rule similar to this:

    routes.MapRoute(
        "WithParam", // Route name
        "{controller}/{action}/{id}" // URL with parameters
    );
    

    Later on using an ActionLink or something similar we can do the following:

    @Html.ActionLink("Details", "Details", new { id = item.IdInt })
    

    And lastly when we press the rendered link, we are sent to the appropriate action which could look something like this:

    public ActionResult Details(int id)
    {
        var item = _session.Load<Item>(id);            
        return View(item);
    }
    

    The reason why this will work is because of the Load method, which takes a ValueType as parameter and then automatically resolves the number to the correct RavenDB id.

    What we have achieved with this approach is a url that looks something like this:

    /items/details/1

    As I said in the beginning, this solution is less elegant than the previous suggested solutions, however, this approach will give you a cleaner url as you can work with the id's as you normally would.

    0 讨论(0)
  • 2021-02-13 05:58

    FYI, the link posted by @jfar was exactly the article. The following is the solution I used from the article:

    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

    0 讨论(0)
提交回复
热议问题