ASP.NET CORE 2.1 FromRoute and FromBody Model Binding












1















I read Microsoft's documentation on building web APIs, but I don't see an example on how to merge route and body params, other than developing a custom model binder. I believe I'm missing something because developing a custom model binder seems to be overkill for this common task. How can I tell my application to create the model from route params and the body payload?



Request:
PUT /business/f8e5cf33-40b1-4b8e-8280-b1b60a459154
{"name": "MyBusiness", "street": "123 Main Street"}

Response:
400
{"Id": ["'Id' must not be empty."]}

// BusinessController
[Route("business/{id}")]
[ApiController]
public class BusinessController : Controller {
[HttpPut]
[ProducesResponseType(400)]
public ActionResult PutAsync(BusinessModel business) {
...
}
}

// BusinessModel
class BusinessModel {
// The `[FromRoute]` annotation has no affect
public Guid Id { get; set; }
public string Name { get; set; }
public string Street { get; set; }
}









share|improve this question



























    1















    I read Microsoft's documentation on building web APIs, but I don't see an example on how to merge route and body params, other than developing a custom model binder. I believe I'm missing something because developing a custom model binder seems to be overkill for this common task. How can I tell my application to create the model from route params and the body payload?



    Request:
    PUT /business/f8e5cf33-40b1-4b8e-8280-b1b60a459154
    {"name": "MyBusiness", "street": "123 Main Street"}

    Response:
    400
    {"Id": ["'Id' must not be empty."]}

    // BusinessController
    [Route("business/{id}")]
    [ApiController]
    public class BusinessController : Controller {
    [HttpPut]
    [ProducesResponseType(400)]
    public ActionResult PutAsync(BusinessModel business) {
    ...
    }
    }

    // BusinessModel
    class BusinessModel {
    // The `[FromRoute]` annotation has no affect
    public Guid Id { get; set; }
    public string Name { get; set; }
    public string Street { get; set; }
    }









    share|improve this question

























      1












      1








      1








      I read Microsoft's documentation on building web APIs, but I don't see an example on how to merge route and body params, other than developing a custom model binder. I believe I'm missing something because developing a custom model binder seems to be overkill for this common task. How can I tell my application to create the model from route params and the body payload?



      Request:
      PUT /business/f8e5cf33-40b1-4b8e-8280-b1b60a459154
      {"name": "MyBusiness", "street": "123 Main Street"}

      Response:
      400
      {"Id": ["'Id' must not be empty."]}

      // BusinessController
      [Route("business/{id}")]
      [ApiController]
      public class BusinessController : Controller {
      [HttpPut]
      [ProducesResponseType(400)]
      public ActionResult PutAsync(BusinessModel business) {
      ...
      }
      }

      // BusinessModel
      class BusinessModel {
      // The `[FromRoute]` annotation has no affect
      public Guid Id { get; set; }
      public string Name { get; set; }
      public string Street { get; set; }
      }









      share|improve this question














      I read Microsoft's documentation on building web APIs, but I don't see an example on how to merge route and body params, other than developing a custom model binder. I believe I'm missing something because developing a custom model binder seems to be overkill for this common task. How can I tell my application to create the model from route params and the body payload?



      Request:
      PUT /business/f8e5cf33-40b1-4b8e-8280-b1b60a459154
      {"name": "MyBusiness", "street": "123 Main Street"}

      Response:
      400
      {"Id": ["'Id' must not be empty."]}

      // BusinessController
      [Route("business/{id}")]
      [ApiController]
      public class BusinessController : Controller {
      [HttpPut]
      [ProducesResponseType(400)]
      public ActionResult PutAsync(BusinessModel business) {
      ...
      }
      }

      // BusinessModel
      class BusinessModel {
      // The `[FromRoute]` annotation has no affect
      public Guid Id { get; set; }
      public string Name { get; set; }
      public string Street { get; set; }
      }






      asp.net asp.net-web-api model-binding






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Jan 18 at 13:56









      DaveDave

      1133




      1133
























          1 Answer
          1






          active

          oldest

          votes


















          0














          You can separate in two tags on the parameter section.



             // BusinessController
          [Route("business/{id}")]
          [ApiController]
          public class BusinessController : Controller {
          [HttpPut]
          [ProducesResponseType(400)]
          public ActionResult PutAsync([FromBody]BusinessModel business, [FromRoute] int id) {
          ...
          }
          }


          And after that asign that id to the model, if it is part of it.






          share|improve this answer
























          • I'm trying to leverage the "Automatic HTTP 400 Responses" functionality. This functionality allows me to remove if(!ModelState.IsValid) checks in the action. So the API is returning a 400 before the action runs. docs.microsoft.com/en-us/aspnet/core/web-api/…

            – Dave
            Jan 18 at 15:00













          • Ok, now I get that if you take off the tags on the parameter section it should do that merge that you want, so it appears that it doesn't do it because your id is a Guid object, and it can't resolve that Guid construction only from the id.

            – nicoflecap1
            Jan 18 at 15:07













          • I've tried with Guid, string, and ints without success.

            – Dave
            Jan 18 at 15:17











          • What happens if you send some int on the Request with that same code with the id like Guid? In your request example the id is missing.

            – nicoflecap1
            Jan 18 at 15:25













          • No matter the data type, I receive a message stating the Id is required. In the Guid example above, f8e5cf33-40b1-4b8e-8280-b1b60a459154 is the id param.

            – Dave
            Jan 18 at 15:32











          Your Answer






          StackExchange.ifUsing("editor", function () {
          StackExchange.using("externalEditor", function () {
          StackExchange.using("snippets", function () {
          StackExchange.snippets.init();
          });
          });
          }, "code-snippets");

          StackExchange.ready(function() {
          var channelOptions = {
          tags: "".split(" "),
          id: "1"
          };
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function() {
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled) {
          StackExchange.using("snippets", function() {
          createEditor();
          });
          }
          else {
          createEditor();
          }
          });

          function createEditor() {
          StackExchange.prepareEditor({
          heartbeatType: 'answer',
          autoActivateHeartbeat: false,
          convertImagesToLinks: true,
          noModals: true,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: 10,
          bindNavPrevention: true,
          postfix: "",
          imageUploader: {
          brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
          contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
          allowUrls: true
          },
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          });


          }
          });














          draft saved

          draft discarded


















          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255492%2fasp-net-core-2-1-fromroute-and-frombody-model-binding%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          0














          You can separate in two tags on the parameter section.



             // BusinessController
          [Route("business/{id}")]
          [ApiController]
          public class BusinessController : Controller {
          [HttpPut]
          [ProducesResponseType(400)]
          public ActionResult PutAsync([FromBody]BusinessModel business, [FromRoute] int id) {
          ...
          }
          }


          And after that asign that id to the model, if it is part of it.






          share|improve this answer
























          • I'm trying to leverage the "Automatic HTTP 400 Responses" functionality. This functionality allows me to remove if(!ModelState.IsValid) checks in the action. So the API is returning a 400 before the action runs. docs.microsoft.com/en-us/aspnet/core/web-api/…

            – Dave
            Jan 18 at 15:00













          • Ok, now I get that if you take off the tags on the parameter section it should do that merge that you want, so it appears that it doesn't do it because your id is a Guid object, and it can't resolve that Guid construction only from the id.

            – nicoflecap1
            Jan 18 at 15:07













          • I've tried with Guid, string, and ints without success.

            – Dave
            Jan 18 at 15:17











          • What happens if you send some int on the Request with that same code with the id like Guid? In your request example the id is missing.

            – nicoflecap1
            Jan 18 at 15:25













          • No matter the data type, I receive a message stating the Id is required. In the Guid example above, f8e5cf33-40b1-4b8e-8280-b1b60a459154 is the id param.

            – Dave
            Jan 18 at 15:32
















          0














          You can separate in two tags on the parameter section.



             // BusinessController
          [Route("business/{id}")]
          [ApiController]
          public class BusinessController : Controller {
          [HttpPut]
          [ProducesResponseType(400)]
          public ActionResult PutAsync([FromBody]BusinessModel business, [FromRoute] int id) {
          ...
          }
          }


          And after that asign that id to the model, if it is part of it.






          share|improve this answer
























          • I'm trying to leverage the "Automatic HTTP 400 Responses" functionality. This functionality allows me to remove if(!ModelState.IsValid) checks in the action. So the API is returning a 400 before the action runs. docs.microsoft.com/en-us/aspnet/core/web-api/…

            – Dave
            Jan 18 at 15:00













          • Ok, now I get that if you take off the tags on the parameter section it should do that merge that you want, so it appears that it doesn't do it because your id is a Guid object, and it can't resolve that Guid construction only from the id.

            – nicoflecap1
            Jan 18 at 15:07













          • I've tried with Guid, string, and ints without success.

            – Dave
            Jan 18 at 15:17











          • What happens if you send some int on the Request with that same code with the id like Guid? In your request example the id is missing.

            – nicoflecap1
            Jan 18 at 15:25













          • No matter the data type, I receive a message stating the Id is required. In the Guid example above, f8e5cf33-40b1-4b8e-8280-b1b60a459154 is the id param.

            – Dave
            Jan 18 at 15:32














          0












          0








          0







          You can separate in two tags on the parameter section.



             // BusinessController
          [Route("business/{id}")]
          [ApiController]
          public class BusinessController : Controller {
          [HttpPut]
          [ProducesResponseType(400)]
          public ActionResult PutAsync([FromBody]BusinessModel business, [FromRoute] int id) {
          ...
          }
          }


          And after that asign that id to the model, if it is part of it.






          share|improve this answer













          You can separate in two tags on the parameter section.



             // BusinessController
          [Route("business/{id}")]
          [ApiController]
          public class BusinessController : Controller {
          [HttpPut]
          [ProducesResponseType(400)]
          public ActionResult PutAsync([FromBody]BusinessModel business, [FromRoute] int id) {
          ...
          }
          }


          And after that asign that id to the model, if it is part of it.







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 18 at 14:49









          nicoflecap1nicoflecap1

          888




          888













          • I'm trying to leverage the "Automatic HTTP 400 Responses" functionality. This functionality allows me to remove if(!ModelState.IsValid) checks in the action. So the API is returning a 400 before the action runs. docs.microsoft.com/en-us/aspnet/core/web-api/…

            – Dave
            Jan 18 at 15:00













          • Ok, now I get that if you take off the tags on the parameter section it should do that merge that you want, so it appears that it doesn't do it because your id is a Guid object, and it can't resolve that Guid construction only from the id.

            – nicoflecap1
            Jan 18 at 15:07













          • I've tried with Guid, string, and ints without success.

            – Dave
            Jan 18 at 15:17











          • What happens if you send some int on the Request with that same code with the id like Guid? In your request example the id is missing.

            – nicoflecap1
            Jan 18 at 15:25













          • No matter the data type, I receive a message stating the Id is required. In the Guid example above, f8e5cf33-40b1-4b8e-8280-b1b60a459154 is the id param.

            – Dave
            Jan 18 at 15:32



















          • I'm trying to leverage the "Automatic HTTP 400 Responses" functionality. This functionality allows me to remove if(!ModelState.IsValid) checks in the action. So the API is returning a 400 before the action runs. docs.microsoft.com/en-us/aspnet/core/web-api/…

            – Dave
            Jan 18 at 15:00













          • Ok, now I get that if you take off the tags on the parameter section it should do that merge that you want, so it appears that it doesn't do it because your id is a Guid object, and it can't resolve that Guid construction only from the id.

            – nicoflecap1
            Jan 18 at 15:07













          • I've tried with Guid, string, and ints without success.

            – Dave
            Jan 18 at 15:17











          • What happens if you send some int on the Request with that same code with the id like Guid? In your request example the id is missing.

            – nicoflecap1
            Jan 18 at 15:25













          • No matter the data type, I receive a message stating the Id is required. In the Guid example above, f8e5cf33-40b1-4b8e-8280-b1b60a459154 is the id param.

            – Dave
            Jan 18 at 15:32

















          I'm trying to leverage the "Automatic HTTP 400 Responses" functionality. This functionality allows me to remove if(!ModelState.IsValid) checks in the action. So the API is returning a 400 before the action runs. docs.microsoft.com/en-us/aspnet/core/web-api/…

          – Dave
          Jan 18 at 15:00







          I'm trying to leverage the "Automatic HTTP 400 Responses" functionality. This functionality allows me to remove if(!ModelState.IsValid) checks in the action. So the API is returning a 400 before the action runs. docs.microsoft.com/en-us/aspnet/core/web-api/…

          – Dave
          Jan 18 at 15:00















          Ok, now I get that if you take off the tags on the parameter section it should do that merge that you want, so it appears that it doesn't do it because your id is a Guid object, and it can't resolve that Guid construction only from the id.

          – nicoflecap1
          Jan 18 at 15:07







          Ok, now I get that if you take off the tags on the parameter section it should do that merge that you want, so it appears that it doesn't do it because your id is a Guid object, and it can't resolve that Guid construction only from the id.

          – nicoflecap1
          Jan 18 at 15:07















          I've tried with Guid, string, and ints without success.

          – Dave
          Jan 18 at 15:17





          I've tried with Guid, string, and ints without success.

          – Dave
          Jan 18 at 15:17













          What happens if you send some int on the Request with that same code with the id like Guid? In your request example the id is missing.

          – nicoflecap1
          Jan 18 at 15:25







          What happens if you send some int on the Request with that same code with the id like Guid? In your request example the id is missing.

          – nicoflecap1
          Jan 18 at 15:25















          No matter the data type, I receive a message stating the Id is required. In the Guid example above, f8e5cf33-40b1-4b8e-8280-b1b60a459154 is the id param.

          – Dave
          Jan 18 at 15:32





          No matter the data type, I receive a message stating the Id is required. In the Guid example above, f8e5cf33-40b1-4b8e-8280-b1b60a459154 is the id param.

          – Dave
          Jan 18 at 15:32


















          draft saved

          draft discarded




















































          Thanks for contributing an answer to Stack Overflow!


          • Please be sure to answer the question. Provide details and share your research!

          But avoid



          • Asking for help, clarification, or responding to other answers.

          • Making statements based on opinion; back them up with references or personal experience.


          To learn more, see our tips on writing great answers.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54255492%2fasp-net-core-2-1-fromroute-and-frombody-model-binding%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown





















































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown

































          Required, but never shown














          Required, but never shown












          Required, but never shown







          Required, but never shown







          Popular posts from this blog

          Callistus III

          Plistias Cous

          Index Sanctorum