what is the difference between those 2 Post methods
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
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
add a comment |
up vote
2
down vote
favorite
up vote
2
down vote
favorite
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
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
c# asp.net-core .net-core
edited 2 days ago
Panagiotis Kanavos
52k478107
52k478107
asked Nov 19 at 11:36
Gru
142
142
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
accepted
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.
Thanks man, and if i may ask, which one is better in terms of performance or best practice ?
– Gru
Nov 19 at 12:14
Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doingGetFile
on aCollection
. 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 at 12:16
@Gru the second one can't be tested at all. It's not only about performance.
– Panagiotis Kanavos
2 days ago
@Gru the methods are different. The first one posts only the file to thePost
action. The second tries to post more data along with the file through theRequestModel
object.
– Panagiotis Kanavos
2 days ago
@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 theOpenReadStream()
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
2 days ago
|
show 2 more comments
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
accepted
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.
Thanks man, and if i may ask, which one is better in terms of performance or best practice ?
– Gru
Nov 19 at 12:14
Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doingGetFile
on aCollection
. 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 at 12:16
@Gru the second one can't be tested at all. It's not only about performance.
– Panagiotis Kanavos
2 days ago
@Gru the methods are different. The first one posts only the file to thePost
action. The second tries to post more data along with the file through theRequestModel
object.
– Panagiotis Kanavos
2 days ago
@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 theOpenReadStream()
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
2 days ago
|
show 2 more comments
up vote
0
down vote
accepted
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.
Thanks man, and if i may ask, which one is better in terms of performance or best practice ?
– Gru
Nov 19 at 12:14
Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doingGetFile
on aCollection
. 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 at 12:16
@Gru the second one can't be tested at all. It's not only about performance.
– Panagiotis Kanavos
2 days ago
@Gru the methods are different. The first one posts only the file to thePost
action. The second tries to post more data along with the file through theRequestModel
object.
– Panagiotis Kanavos
2 days ago
@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 theOpenReadStream()
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
2 days ago
|
show 2 more comments
up vote
0
down vote
accepted
up vote
0
down vote
accepted
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.
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.
edited 2 days ago
answered Nov 19 at 11:41


Adriani6
4,01821023
4,01821023
Thanks man, and if i may ask, which one is better in terms of performance or best practice ?
– Gru
Nov 19 at 12:14
Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doingGetFile
on aCollection
. 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 at 12:16
@Gru the second one can't be tested at all. It's not only about performance.
– Panagiotis Kanavos
2 days ago
@Gru the methods are different. The first one posts only the file to thePost
action. The second tries to post more data along with the file through theRequestModel
object.
– Panagiotis Kanavos
2 days ago
@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 theOpenReadStream()
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
2 days ago
|
show 2 more comments
Thanks man, and if i may ask, which one is better in terms of performance or best practice ?
– Gru
Nov 19 at 12:14
Depending on what you're doing - performance ways - it's obviously faster to directly access the object rather than doingGetFile
on aCollection
. 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 at 12:16
@Gru the second one can't be tested at all. It's not only about performance.
– Panagiotis Kanavos
2 days ago
@Gru the methods are different. The first one posts only the file to thePost
action. The second tries to post more data along with the file through theRequestModel
object.
– Panagiotis Kanavos
2 days ago
@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 theOpenReadStream()
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
2 days ago
Thanks man, and if i may ask, which one is better in terms of performance or best practice ?
– Gru
Nov 19 at 12:14
Thanks man, and if i may ask, which one is better in terms of performance or best practice ?
– Gru
Nov 19 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 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 at 12:16
@Gru the second one can't be tested at all. It's not only about performance.
– Panagiotis Kanavos
2 days ago
@Gru the second one can't be tested at all. It's not only about performance.
– Panagiotis Kanavos
2 days ago
@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
2 days ago
@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
2 days ago
@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
2 days ago
@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
2 days ago
|
show 2 more comments
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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