This repository was archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServerCommand3.cs
More file actions
96 lines (85 loc) · 3.34 KB
/
ServerCommand3.cs
File metadata and controls
96 lines (85 loc) · 3.34 KB
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
using GrapeCity.Forguncy.Commands;
using GrapeCity.Forguncy.Plugin;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace RetreiveFieldFormHTTPHead
{
/// <summary>
/// 插件的类。
/// Command:插件的基类,适用于所有类型插件
/// ICommandExecutableInServerSide:服务端命令专用的接口
/// IServerCommandParamGenerator:需要将返回值写入变量的插件专用的接口
/// Icon:设置在设计器中使用的图标,需要将其打包到Resources
/// </summary>
[Icon("pack://application:,,,/RetreiveFieldFormHTTPHead;component/Resources/Icon.png")]
public class WriteFieldToHTTPHeadCommand : Command, ICommandExecutableInServerSide, IServerCommandParamGenerator
{
/// <summary>
/// 插件类型:设置为服务端命令插件
/// </summary>
/// <returns>插件类型枚举</returns>
public override CommandScope GetCommandScope()
{
return CommandScope.ServerSide;
}
/// <summary>
/// 在设计器中展示的插件名称
/// </summary>
/// <returns>易读的字符串</returns>
public override string ToString()
{
if (null == FieldName) {
return "写入HTTP响应标头";
} else {
return "写入HTTP响应标头:" + FieldName.ToString();
}
}
[DisplayName("要写入的标头:"), FormulaProperty(true)]
public object FieldName { get; set; }
[DisplayName("值:"), FormulaProperty(true)]
public string ParamaterName4FieldValue { get; set; }
/// <summary>
/// 命令执行逻辑
/// </summary>
/// <param name="dataContext">用来操作的上下文,包含HTTP请求的上下文、数据上下文等</param>
/// <returns></returns>
public ExecuteResult Execute(IServerCommandExecuteContext dataContext)
{
try
{
string key = dataContext.EvaluateFormulaAsync(this.FieldName).Result.ToString();
string value = dataContext.EvaluateFormulaAsync(this.ParamaterName4FieldValue).Result.ToString();
if (dataContext.Context.Response.Headers.ContainsKey(key))
{
dataContext.Context.Response.Headers[key] = value;
}
else
{
dataContext.Context.Response.Headers.Add(key, value);
}
return new ExecuteResult();
}
catch (Exception exception)
{
dataContext.Log.AppendLine("【写入HTTP响应标头】的Execute方法发生异常:\r\n" + exception.ToString(), Array.Empty<string>());
return new ExecuteResult();
}
}
/// <summary>
/// 登记用来返回处理结果的参数名
/// 这个值是在运行时确定的,推荐使用yield来处理
/// </summary>
/// <returns></returns>
public IEnumerable<GenerateParam> GetGenerateParams()
{
yield return new GenerateNormalParam()
{
ParamName = ParamaterName4FieldValue,
};
}
}
}