MVC controller basis of common return type

Hello everyone, I am a child Dacheng, write it again MVC controller common type of return today, to sum up the knowledge of the past.

IActionResult

IActionResult is not to say that it is all Result of the parent class. Here to talk about several common.

ViewResult

For outputting the contents of a view:

 

 

ContentResult

Simple text output:

 

 

 

 

JsonResult

Json string output

 

 

Jump control of redirection

Redirect: Redirect page Jump

 

 

RedirectToAction: Jump to specified action (this can be non-controller)

 

 

RedirectToRoute: jump value specified route to the specified route

 

 

File Output

Specified file path to the output file:

Note: With this method, not related to the stream file, does not need to read files on the hard disk, the virtual path used directly, i.e., the relative path

 

 

Test results are as follows:

 

 

Using the byte array output file:

 

Tests are as follows:

 

This is the online browsing, should the need to download way, then the method returns to add a third parameter, named downloaded file:

 

 

 

 

Using streamed output file:

 

 

 


to sum up:

1. Benpian the more common types article illustrates the MVC / API returned, but whether ViewResult ContentResult or all of their base class for all types of IActionResult, do not have to write specific type of return in writing, direct write IActionResult.

2. You can also write the usual we see int, string, or other types or void also

3. redirection.

        Redirect (Page path)

        The RedirectToAction (redirected to the specified controller, operation method)

        RedirectToRoute (jump value specified route)

4. The focus is on knowledge download file upload and download in the final, which is the output file resources.

(1) directly in the path of the resource items, read the file, the output file through the path. Note: You need to use a virtual (relative) path.

(2) using the byte array, the output file resources. Need to use physical (absolute) path.

(3) using a stream, the output file resources. Need to use physical (absolute path).

Micro-channel public number: dotNET Learning space

好了,今天的学习就到这里结束了,有需要交流的请联系小编,共同学习进步。关于文件资源上传的,我们以后的学习再讲解。MVC和API原理差不多,返回类型也差不多,所以上述讲解的MVC中的全部内容在API中也同样适用。

      代码地址:https://github.com/huguangcheng/_NET-Core_Study/tree/master/FileUpAndDown

Guess you like

Origin www.cnblogs.com/xingxingzhou/p/11992330.html
Recommended