From 1df155259da7d2c11750fa966510772285f693fe Mon Sep 17 00:00:00 2001 From: Elivo Date: Sun, 3 Aug 2025 23:38:59 +0800 Subject: [PATCH] =?UTF-8?q?TextResult=EF=BC=8C=E6=94=AF=E6=8C=81=E4=BF=AE?= =?UTF-8?q?=E6=94=B9=20Text=20=E5=B1=9E=E6=80=A7=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Apewer/Web/BytesResult.cs | 12 +++++------- Apewer/Web/TextResult.cs | 20 ++++++++++++++++---- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/Apewer/Web/BytesResult.cs b/Apewer/Web/BytesResult.cs index 52378eb..9cc74a6 100644 --- a/Apewer/Web/BytesResult.cs +++ b/Apewer/Web/BytesResult.cs @@ -1,8 +1,4 @@ -using System; -using System.Collections.Generic; -using System.Text; - -namespace Apewer.Web +namespace Apewer.Web { /// 表示 API 行为结果,主体为字节数组。 @@ -11,8 +7,10 @@ namespace Apewer.Web #region content + byte[] _bytes; + /// 主体。 - public virtual byte[] Bytes { get; set; } + public virtual byte[] Bytes { get => _bytes; set => _bytes = value; } /// 创建结果实例。 public BytesResult(byte[] bytes, string contentType = "application/octet-stream") : this(200, bytes, contentType) { } @@ -21,7 +19,7 @@ namespace Apewer.Web public BytesResult(int status, byte[] bytes, string contentType = "application/octet-stream") : base(status) { Headers.Add("Content-Type", contentType); - Bytes = bytes; + _bytes = bytes; } #endregion diff --git a/Apewer/Web/TextResult.cs b/Apewer/Web/TextResult.cs index 2bbad31..51310ac 100644 --- a/Apewer/Web/TextResult.cs +++ b/Apewer/Web/TextResult.cs @@ -1,6 +1,4 @@ using System; -using System.Collections.Generic; -using System.Text; namespace Apewer.Web { @@ -9,11 +7,25 @@ namespace Apewer.Web public class TextResult : BytesResult { + string _text; + + /// Body 文本。 + public virtual string Text { get => _text; set => SetText(value); } + + /// Body 字节数组。 + public override byte[] Bytes { get => base.Bytes; set => throw new NotSupportedException(); } + /// 创建结果实例。 - public TextResult(string text, string contentType = "text/plain") : base(text.Bytes(), contentType) { } + public TextResult(string text, string contentType = "text/plain") : base(null, contentType) => SetText(text); /// 创建结果实例。 - public TextResult(int status, string text, string contentType = "text/plain") : base(status, text.Bytes(), contentType) { } + public TextResult(int status, string text, string contentType = "text/plain") : base(status, null, contentType) => SetText(text); + + void SetText(string text) + { + _text = text; + base.Bytes = text.Bytes(); + } }