Browse Source

TextResult,支持修改 Text 属性。

master
王厅 4 days ago
parent
commit
1df155259d
  1. 12
      Apewer/Web/BytesResult.cs
  2. 20
      Apewer/Web/TextResult.cs

12
Apewer/Web/BytesResult.cs

@ -1,8 +1,4 @@
using System; namespace Apewer.Web
using System.Collections.Generic;
using System.Text;
namespace Apewer.Web
{ {
/// <summary>表示 API 行为结果,主体为字节数组。</summary> /// <summary>表示 API 行为结果,主体为字节数组。</summary>
@ -11,8 +7,10 @@ namespace Apewer.Web
#region content #region content
byte[] _bytes;
/// <summary>主体。</summary> /// <summary>主体。</summary>
public virtual byte[] Bytes { get; set; } public virtual byte[] Bytes { get => _bytes; set => _bytes = value; }
/// <summary>创建结果实例。</summary> /// <summary>创建结果实例。</summary>
public BytesResult(byte[] bytes, string contentType = "application/octet-stream") : this(200, bytes, contentType) { } 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) public BytesResult(int status, byte[] bytes, string contentType = "application/octet-stream") : base(status)
{ {
Headers.Add("Content-Type", contentType); Headers.Add("Content-Type", contentType);
Bytes = bytes; _bytes = bytes;
} }
#endregion #endregion

20
Apewer/Web/TextResult.cs

@ -1,6 +1,4 @@
using System; using System;
using System.Collections.Generic;
using System.Text;
namespace Apewer.Web namespace Apewer.Web
{ {
@ -9,11 +7,25 @@ namespace Apewer.Web
public class TextResult : BytesResult public class TextResult : BytesResult
{ {
string _text;
/// <summary>Body 文本。</summary>
public virtual string Text { get => _text; set => SetText(value); }
/// <summary>Body 字节数组。</summary>
public override byte[] Bytes { get => base.Bytes; set => throw new NotSupportedException(); }
/// <summary>创建结果实例。</summary> /// <summary>创建结果实例。</summary>
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);
/// <summary>创建结果实例。</summary> /// <summary>创建结果实例。</summary>
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();
}
} }

Loading…
Cancel
Save