what is the difference between those 2 Post methods












2















am a bit confused about the difference between this 2 lines:



req.OpenReadStream();


and



Request.Form.Files.GetFile("FileContent").OpenReadStream()


here is what i know so far and correct me if am wrong, i know that they are both meant to read a file, and the first method accept file only, however the second accept file and a json value,,
but what i still dont understand is the difference in term of syntax



Here is a snippet from the post methods:



public IActionResult Post(IFormFile req)
{
req.OpenReadStream();
return Ok();
}




[HttpPost]
public IActionResult Post([FromForm] RequestModel req)
{
Request.Form.Files.GetFile("FileContent").OpenReadStream()
return Ok();
}

//....
public class RequestModel
{
public string FileContent { get; set; }
public string SomeRandomString { get; set; }
}









share|improve this question





























    2















    am a bit confused about the difference between this 2 lines:



    req.OpenReadStream();


    and



    Request.Form.Files.GetFile("FileContent").OpenReadStream()


    here is what i know so far and correct me if am wrong, i know that they are both meant to read a file, and the first method accept file only, however the second accept file and a json value,,
    but what i still dont understand is the difference in term of syntax



    Here is a snippet from the post methods:



    public IActionResult Post(IFormFile req)
    {
    req.OpenReadStream();
    return Ok();
    }




    [HttpPost]
    public IActionResult Post([FromForm] RequestModel req)
    {
    Request.Form.Files.GetFile("FileContent").OpenReadStream()
    return Ok();
    }

    //....
    public class RequestModel
    {
    public string FileContent { get; set; }
    public string SomeRandomString { get; set; }
    }









    share|improve this question



























      2












      2








      2


      1






      am a bit confused about the difference between this 2 lines:



      req.OpenReadStream();


      and



      Request.Form.Files.GetFile("FileContent").OpenReadStream()


      here is what i know so far and correct me if am wrong, i know that they are both meant to read a file, and the first method accept file only, however the second accept file and a json value,,
      but what i still dont understand is the difference in term of syntax



      Here is a snippet from the post methods:



      public IActionResult Post(IFormFile req)
      {
      req.OpenReadStream();
      return Ok();
      }




      [HttpPost]
      public IActionResult Post([FromForm] RequestModel req)
      {
      Request.Form.Files.GetFile("FileContent").OpenReadStream()
      return Ok();
      }

      //....
      public class RequestModel
      {
      public string FileContent { get; set; }
      public string SomeRandomString { get; set; }
      }









      share|improve this question
















      am a bit confused about the difference between this 2 lines:



      req.OpenReadStream();


      and



      Request.Form.Files.GetFile("FileContent").OpenReadStream()


      here is what i know so far and correct me if am wrong, i know that they are both meant to read a file, and the first method accept file only, however the second accept file and a json value,,
      but what i still dont understand is the difference in term of syntax



      Here is a snippet from the post methods:



      public IActionResult Post(IFormFile req)
      {
      req.OpenReadStream();
      return Ok();
      }




      [HttpPost]
      public IActionResult Post([FromForm] RequestModel req)
      {
      Request.Form.Files.GetFile("FileContent").OpenReadStream()
      return Ok();
      }

      //....
      public class RequestModel
      {
      public string FileContent { get; set; }
      public string SomeRandomString { get; set; }
      }






      c# asp.net-core .net-core






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 '18 at 12:51









      Panagiotis Kanavos

      55.1k482109




      55.1k482109










      asked Nov 19 '18 at 11:36









      GruGru

      344




      344
























          1 Answer
          1






          active

          oldest

          votes


















          0














          None. They both belong to IFormFile.



          The only difference is how you're accessing the interface.



          In your first example you're accessing it directly as it's passed into the constructor of your method.



          In your second example you're accessing it from the Files collection of the HttpRequest by getting the file using GetFile method that returns the said interface.



          As Panagiotis Kanavos said, the later can not be tested at all.






          share|improve this answer


























          • Thanks man, and if i may ask, which one is better in terms of performance or best practice ?

            – Gru
            Nov 19 '18 at 12:14











          • Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doing GetFile on a Collection. But that's not always possible depending on the scenario like yours. I wouldn't worry too much about this overhead, as I said it's minimal.

            – Adriani6
            Nov 19 '18 at 12:16













          • @Gru the second one can't be tested at all. It's not only about performance.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:50











          • @Gru the methods are different. The first one posts only the file to the Post action. The second tries to post more data along with the file through the RequestModel object.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:51











          • @PanagiotisKanavos I think we're talking about two different things here. The OP didn't ask about his methods on the controller but about the difference in how he accesses the OpenReadStream() method. What you said is true, but what you said in your second comment - OP already knows about it as per his statement in the question.

            – Adriani6
            Nov 19 '18 at 12:53













          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%2f53373809%2fwhat-is-the-difference-between-those-2-post-methods%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














          None. They both belong to IFormFile.



          The only difference is how you're accessing the interface.



          In your first example you're accessing it directly as it's passed into the constructor of your method.



          In your second example you're accessing it from the Files collection of the HttpRequest by getting the file using GetFile method that returns the said interface.



          As Panagiotis Kanavos said, the later can not be tested at all.






          share|improve this answer


























          • Thanks man, and if i may ask, which one is better in terms of performance or best practice ?

            – Gru
            Nov 19 '18 at 12:14











          • Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doing GetFile on a Collection. But that's not always possible depending on the scenario like yours. I wouldn't worry too much about this overhead, as I said it's minimal.

            – Adriani6
            Nov 19 '18 at 12:16













          • @Gru the second one can't be tested at all. It's not only about performance.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:50











          • @Gru the methods are different. The first one posts only the file to the Post action. The second tries to post more data along with the file through the RequestModel object.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:51











          • @PanagiotisKanavos I think we're talking about two different things here. The OP didn't ask about his methods on the controller but about the difference in how he accesses the OpenReadStream() method. What you said is true, but what you said in your second comment - OP already knows about it as per his statement in the question.

            – Adriani6
            Nov 19 '18 at 12:53


















          0














          None. They both belong to IFormFile.



          The only difference is how you're accessing the interface.



          In your first example you're accessing it directly as it's passed into the constructor of your method.



          In your second example you're accessing it from the Files collection of the HttpRequest by getting the file using GetFile method that returns the said interface.



          As Panagiotis Kanavos said, the later can not be tested at all.






          share|improve this answer


























          • Thanks man, and if i may ask, which one is better in terms of performance or best practice ?

            – Gru
            Nov 19 '18 at 12:14











          • Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doing GetFile on a Collection. But that's not always possible depending on the scenario like yours. I wouldn't worry too much about this overhead, as I said it's minimal.

            – Adriani6
            Nov 19 '18 at 12:16













          • @Gru the second one can't be tested at all. It's not only about performance.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:50











          • @Gru the methods are different. The first one posts only the file to the Post action. The second tries to post more data along with the file through the RequestModel object.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:51











          • @PanagiotisKanavos I think we're talking about two different things here. The OP didn't ask about his methods on the controller but about the difference in how he accesses the OpenReadStream() method. What you said is true, but what you said in your second comment - OP already knows about it as per his statement in the question.

            – Adriani6
            Nov 19 '18 at 12:53
















          0












          0








          0







          None. They both belong to IFormFile.



          The only difference is how you're accessing the interface.



          In your first example you're accessing it directly as it's passed into the constructor of your method.



          In your second example you're accessing it from the Files collection of the HttpRequest by getting the file using GetFile method that returns the said interface.



          As Panagiotis Kanavos said, the later can not be tested at all.






          share|improve this answer















          None. They both belong to IFormFile.



          The only difference is how you're accessing the interface.



          In your first example you're accessing it directly as it's passed into the constructor of your method.



          In your second example you're accessing it from the Files collection of the HttpRequest by getting the file using GetFile method that returns the said interface.



          As Panagiotis Kanavos said, the later can not be tested at all.







          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 19 '18 at 12:58

























          answered Nov 19 '18 at 11:41









          Adriani6Adriani6

          4,57021527




          4,57021527













          • Thanks man, and if i may ask, which one is better in terms of performance or best practice ?

            – Gru
            Nov 19 '18 at 12:14











          • Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doing GetFile on a Collection. But that's not always possible depending on the scenario like yours. I wouldn't worry too much about this overhead, as I said it's minimal.

            – Adriani6
            Nov 19 '18 at 12:16













          • @Gru the second one can't be tested at all. It's not only about performance.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:50











          • @Gru the methods are different. The first one posts only the file to the Post action. The second tries to post more data along with the file through the RequestModel object.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:51











          • @PanagiotisKanavos I think we're talking about two different things here. The OP didn't ask about his methods on the controller but about the difference in how he accesses the OpenReadStream() method. What you said is true, but what you said in your second comment - OP already knows about it as per his statement in the question.

            – Adriani6
            Nov 19 '18 at 12:53





















          • Thanks man, and if i may ask, which one is better in terms of performance or best practice ?

            – Gru
            Nov 19 '18 at 12:14











          • Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doing GetFile on a Collection. But that's not always possible depending on the scenario like yours. I wouldn't worry too much about this overhead, as I said it's minimal.

            – Adriani6
            Nov 19 '18 at 12:16













          • @Gru the second one can't be tested at all. It's not only about performance.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:50











          • @Gru the methods are different. The first one posts only the file to the Post action. The second tries to post more data along with the file through the RequestModel object.

            – Panagiotis Kanavos
            Nov 19 '18 at 12:51











          • @PanagiotisKanavos I think we're talking about two different things here. The OP didn't ask about his methods on the controller but about the difference in how he accesses the OpenReadStream() method. What you said is true, but what you said in your second comment - OP already knows about it as per his statement in the question.

            – Adriani6
            Nov 19 '18 at 12:53



















          Thanks man, and if i may ask, which one is better in terms of performance or best practice ?

          – Gru
          Nov 19 '18 at 12:14





          Thanks man, and if i may ask, which one is better in terms of performance or best practice ?

          – Gru
          Nov 19 '18 at 12:14













          Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doing GetFile on a Collection. But that's not always possible depending on the scenario like yours. I wouldn't worry too much about this overhead, as I said it's minimal.

          – Adriani6
          Nov 19 '18 at 12:16







          Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doing GetFile on a Collection. But that's not always possible depending on the scenario like yours. I wouldn't worry too much about this overhead, as I said it's minimal.

          – Adriani6
          Nov 19 '18 at 12:16















          @Gru the second one can't be tested at all. It's not only about performance.

          – Panagiotis Kanavos
          Nov 19 '18 at 12:50





          @Gru the second one can't be tested at all. It's not only about performance.

          – Panagiotis Kanavos
          Nov 19 '18 at 12:50













          @Gru the methods are different. The first one posts only the file to the Post action. The second tries to post more data along with the file through the RequestModel object.

          – Panagiotis Kanavos
          Nov 19 '18 at 12:51





          @Gru the methods are different. The first one posts only the file to the Post action. The second tries to post more data along with the file through the RequestModel object.

          – Panagiotis Kanavos
          Nov 19 '18 at 12:51













          @PanagiotisKanavos I think we're talking about two different things here. The OP didn't ask about his methods on the controller but about the difference in how he accesses the OpenReadStream() method. What you said is true, but what you said in your second comment - OP already knows about it as per his statement in the question.

          – Adriani6
          Nov 19 '18 at 12:53







          @PanagiotisKanavos I think we're talking about two different things here. The OP didn't ask about his methods on the controller but about the difference in how he accesses the OpenReadStream() method. What you said is true, but what you said in your second comment - OP already knows about it as per his statement in the question.

          – Adriani6
          Nov 19 '18 at 12:53






















          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%2f53373809%2fwhat-is-the-difference-between-those-2-post-methods%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







          這個網誌中的熱門文章

          Tangent Lines Diagram Along Smooth Curve

          Yusuf al-Mu'taman ibn Hud

          Zucchini