简体   繁体   English

这2种Post方法之间有什么区别

[英]what is the difference between those 2 Post methods

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 这是到目前为止我所知道的,如果有错,请纠正我,我知道它们都旨在读取文件,并且第一种方法仅接受文件,但是第二种接受文件和json值,但是我仍然不知道了解语法方面的差异

Here is a snippet from the post methods: 这是post方法的摘录:

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; }    
}

None. 没有。 They both belong to IFormFile . 它们都属于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. 在第二个示例中,您将使用返回上述接口的GetFile方法获取文件,从而从HttpRequestFiles集合中访问它。

As Panagiotis Kanavos said, the later can not be tested at all. 正如Panagiotis Kanavos所说,后期根本无法测试。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM