Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8231145
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T17:22:58+00:00 2026-06-07T17:22:58+00:00

I’m working on a web API, based on MVC4 RC and using database-first Entity

  • 0

I’m working on a web API, based on MVC4 RC and using database-first Entity Framework as my model.
2 of the entities I have are Item and Group.
There’s a many-to-many relationship between these 2 entities.

Now, after quite easily implementing the API of CRUD operation for both, using the standard HTTP methods (GET, POST, PUT and DELETE), I came to the point in which I want to implement the binding and unbinding of items to and from groups.

I’ve tries other verbs, such as LOCK and UNLOCK, without success (they seem not to support them), and tried to somehow manipulate the POST and the PUT commands, again, without success.

Does any of you good people have an idea how to implement this?

Thanks a lot!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T17:23:00+00:00Added an answer on June 7, 2026 at 5:23 pm

    You can represent the many-to-many as a sub-collection on the root resource. E.g. You have /items/1234 and /groups/4567 – you could have groups as a subcollection as /items/1234/groups or /groups/4567/items

    Either way is equally valid. I usually go the route of using a PUT to set the relationship and a DELETE to remove it – some would say that’s not really REST but it’s worked fine in the scenarios I’ve used it in.

    PUT /items/1234/groups/4567 – create a relationship between item 1234 and group 4567
    DELETE /items/1234/groups/4567 – delete a relationship between item 1234 and group 4567

    This post helped me a lot. When I was looking into this last…

    How to handle many-to-many relationships in a RESTful API?

    Update: Routing

    So for these more complex scenarios we’ve ended up simply using more specific routes. It can get ugly quickly trying to cram everything into a single generic route. We’ve got a suite of unit tests that make sure the relevant URL gets routed to the right controller and action.

        // routes
        routes.MapHttpRoute(
            name: "items.groups",
            routeTemplate: "items/{itemId}/groups/{groupId}",
            defaults: new { controller = "ItemGroup", groupId = RouteParameter.Optional });
    

    The ItemGroupController then has Get, Delete and Put methods. Which we unit test like this…

        // unit tests
        [Test]
        public void PutItemGroup()
        {
            RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Put, "~/items/1234/groups/4567");
            Assert.IsNotNull(routingResult);
            Assert.AreEqual("ItemGroup", routingResult.Controller);
            Assert.AreEqual("Put", routingResult.Action);
            Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
            Assert.AreEqual("4567", routingResult.RouteData.Values["groupId"]);
        }
    
        [Test]
        public void GetItemGroups()
        {
            RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Get, "~/items/1234/groups");
            Assert.IsNotNull(routingResult);
            Assert.AreEqual("ItemGroup", routingResult.Controller);
            Assert.AreEqual("GetAll", routingResult.Action);
            Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
        }
    
        [Test]
        public void GetItemGroup()
        {
            RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Get, "~/items/1234/groups/4567");
            Assert.IsNotNull(routingResult);
            Assert.AreEqual("ItemGroup", routingResult.Controller);
            Assert.AreEqual("Get", routingResult.Action);
            Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
            Assert.AreEqual("4567", routingResult.RouteData.Values["groupId"]);
        }
    
        [Test]
        public void DeleteItemGroup()
        {
            RoutingResult routingResult = this.GenerateRoutingResult(HttpMethod.Delete, "~/items/1234/groups/4567");
            Assert.IsNotNull(routingResult);
            Assert.AreEqual("ItemGroup", routingResult.Controller);
            Assert.AreEqual("Delete", routingResult.Action);
            Assert.AreEqual("1234", routingResult.RouteData.Values["itemId"]);
            Assert.AreEqual("4567", routingResult.RouteData.Values["groupId"]);
        }
    
        private RoutingResult GenerateRoutingResult(HttpMethod method, string relativeUrl)
        {
            HttpConfiguration httpConfiguration = new HttpConfiguration(this.HttpRoutes);
            HttpRequestMessage request = new HttpRequestMessage(method, string.Format("http://test.local/{0}", relativeUrl.Replace("~/", string.Empty)));
            IHttpRouteData routeData = this.HttpRoutes.GetRouteData(request);
    
            Assert.IsNotNull(routeData, "Could not locate route for {0}", relativeUrl);
    
            this.RemoveOptionalRoutingParameters(routeData.Values);
    
            request.Properties.Add(HttpPropertyKeys.HttpRouteDataKey, routeData);
            request.Properties.Add(HttpPropertyKeys.HttpConfigurationKey, httpConfiguration);
    
            IHttpControllerSelector controllerSelector = new DefaultHttpControllerSelector(httpConfiguration);
            HttpControllerContext controllerContext = new HttpControllerContext(httpConfiguration, routeData, request)
                {
                    ControllerDescriptor = controllerSelector.SelectController(request)
                };
    
            HttpActionDescriptor actionDescriptor = controllerContext.ControllerDescriptor.HttpActionSelector.SelectAction(controllerContext);
            if (actionDescriptor == null)
            {
                return null;
            }
    
            return new RoutingResult
                {
                    Action = actionDescriptor.ActionName,
                    Controller = actionDescriptor.ControllerDescriptor.ControllerName,
                    RouteData = routeData
                };
        }
    
        private void RemoveOptionalRoutingParameters(IDictionary<string, object> routeValueDictionary)
        {
            int count = routeValueDictionary.Count;
            int index1 = 0;
            string[] strArray = new string[count];
            foreach (KeyValuePair<string, object> keyValuePair in routeValueDictionary)
            {
                if (keyValuePair.Value == RouteParameter.Optional)
                {
                    strArray[index1] = keyValuePair.Key;
                    ++index1;
                }
            }
    
            for (int index2 = 0; index2 < index1; ++index2)
            {
                string key = strArray[index2];
                routeValueDictionary.Remove(key);
            }
        }
    
        private class RoutingResult
        {
            public string Controller { get; set; }
    
            public string Action { get; set; }
    
            public IHttpRouteData RouteData { get; set; }
        }
    

    Cheers,
    Dean

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm making a simple page using Google Maps API 3. My first. One marker
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I have this code to decode numeric html entities to the UTF8 equivalent character.
We're building an app, our first using Rails 3, and we're having to build
I have thousands of HTML files to process using Groovy/Java and I need to
I have a reasonable size flat file database of text documents mostly saved in
I have a view passing on information from a database: def serve_article(request, id): served_article
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.