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(); + } }