.NET Core第32天_Controller Action的各種不同回傳(FileContentResult,FileStreamResult,VirtualFileResult,PhysicalFileResult,JsonResult,EmptyResult,NoContentResult)_part2

 

FileContentResult

和FileResult不同點在於傳入參數要是binary型態
且結果不會下載而是直接呈現在網頁上

這裡準備預設./wwwroot/css/site.css和./Data/Products.xml兩種檔案


測試的action

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
public FileContentResult DownloadContent_css()
{
    var testFile = System.IO.File.ReadAllBytes("./wwwroot/css/site.css");
    return new FileContentResult(testFile,"text/plain");
}

public FileContentResult DownloadContent_xml()
{
    var testFile = System.IO.File.ReadAllBytes("./Data/Products.xml");
    return new FileContentResult(testFile, "text/xml");
}

效果






FileStreamResult

把文本內容透過stream形式寫入來做檔案下載

1
2
3
4
5
6
7
8
public FileStreamResult CreateFile()
{
    var stream = new System.IO.MemoryStream(System.Text.Encoding.ASCII.GetBytes("Hello FileStreamResult"));
    return new FileStreamResult(stream, new Microsoft.Net.Http.Headers.MediaTypeHeaderValue("text/plain"))
    {
        FileDownloadName = "test.txt"
    };
}


效果







VirtualFileResult PhysicalFileResult

虛擬檔案路徑(相對路徑一定要存放wwwroot下的目錄)
實際硬碟檔案路徑(要指定實際硬碟絕對路徑)

測試程式

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using Net5AppDiffAction.Models;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Threading.Tasks;

namespace Net5AppDiffAction.Controllers
{
    public class HomeController : Controller
    {
        private readonly ILogger<HomeController> _logger;
        private readonly IWebHostEnvironment _environment;

        public HomeController(ILogger<HomeController> logger, IWebHostEnvironment enviroment)
        {
            _logger = logger;
            _environment = enviroment;
        }

        public VirtualFileResult VirtualFileResultDemo()
        {
            return new VirtualFileResult("/css/site.css", "text/plain");
        }

        public PhysicalFileResult ShowProducts()
        {
            return new PhysicalFileResult(_environment.ContentRootPath + "/Data/Products.xml", "text/xml");
        }

        public PhysicalFileResult PhysicalFileResultDemo()
        {
            return new PhysicalFileResult(_environment.ContentRootPath + "/wwwroot/css/site.css", "text/plain");
        }
    }
}


效果




JsonResult

這裡準備創建好一個 model class Products



寫回傳Json result的 action

1
2
3
4
5
public JsonResult ShowNewProducts()
{
    Products prod = new Products() { ProductCode = 101, ProductName = "Printer", Cost = 1500 };
    return Json(prod);
}

藉由JsonResult序列化物件至JSON格式字串



運行效果





EmptyResult 和 NoContentResult

測試action

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
public EmptyResult EmptyResultDemo()
{
    return new EmptyResult();
}


public NoContentResult NoContentResultDemo()
{
    return NoContent();
}




各自差異在於
NoContentResult status code返回204


EmptyResult  status code則是預設的200返回


使用時機
當你不想有任何response顯示在網頁上的時候
而同時也不想throw 出error直接給client端的時候
就會看你是傾向告知user你查詢結果為空(NoContentResult)
或者執行過程有錯(EmptyResult)


若想驗證回傳的status code可以下載Fiddler來自行測試









Ref:


認識 ASP․NET Core 檔案提供者與透過 Web API 下載實體檔案


File Providers in ASP.NET Core

ASP.NET Core MVC returning file using FileResult









留言

這個網誌中的熱門文章

經得起原始碼資安弱點掃描的程式設計習慣培養(五)_Missing HSTS Header

經得起原始碼資安弱點掃描的程式設計習慣培養(三)_7.Cross Site Scripting(XSS)_Stored XSS_Reflected XSS All Clients

(2021年度)駕訓學科筆試準備題庫歸納分析_法規是非題