Skip to content

Commit 3f1ffe1

Browse files
committed
WebApi v1.1.3 添加 AddApiControllerAttribute 选项,默认为 true
1 parent 3abae3f commit 3f1ffe1

File tree

3 files changed

+15
-8
lines changed

3 files changed

+15
-8
lines changed

src/Senparc.CO2NET.WebApi/Senparc.CO2NET.WebApi.csproj

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<Project Sdk="Microsoft.NET.Sdk">
22
<PropertyGroup>
33
<TargetFrameworks>netstandard2.1;netcoreapp3.1;net5.0;net6.0</TargetFrameworks>
4-
<Version>1.1.2</Version>
4+
<Version>1.1.3</Version>
55
<LangVersion>latest</LangVersion>
66
<AssemblyName>Senparc.CO2NET.WebApi</AssemblyName>
77
<RootNamespace>Senparc.CO2NET.WebApi</RootNamespace>
@@ -32,6 +32,7 @@
3232
v0.2.8 提供 .NET Standard 2.1 版本
3333
v1.1 提供参数属性同步复制到动态 Api 的能力
3434
v1.1.2 优化文档提取正则表达式
35+
v1.1.3 添加 AddApiControllerAttribute 选项,默认为 true
3536
</PackageReleaseNotes>
3637
</PropertyGroup>
3738
<PropertyGroup Condition=" '$(Configuration)' == 'Release' ">

src/Senparc.CO2NET.WebApi/WebApiEngines/WebApiEngine.cs

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ public partial class WebApiEngine
5656
private readonly bool _copyCustomAttributes;
5757
private Type _typeOfApiBind = typeof(ApiBindAttribute);
5858
private Type _baseApiControllerType;
59+
private bool _addApiControllerAttribute = true;
5960

6061
public bool BuildXml => DocXmlPath != null;
6162

@@ -77,6 +78,7 @@ public WebApiEngine(Action<WebApiEngineOptions> options = null)
7778
_copyCustomAttributes = opt.CopyCustomAttributes;
7879
TaskCount = opt.TaskCount;
7980
_showDetailApiLog = opt.ShowDetailApiLog;
81+
_addApiControllerAttribute = opt.AddApiControllerAttribute;
8082
Register.ForbiddenExternalAccess = opt.ForbiddenExternalAccess;
8183
WebApiEngine.AdditionalAttributeFunc = opt.AdditionalAttributeFunc;
8284
}
@@ -408,11 +410,11 @@ private async Task BuildApiMethodForOneThread(IGrouping<string, KeyValuePair<str
408410
//{
409411
// if (boundClassType == false)
410412
// {
411-
413+
412414
// }
413415
// else
414416
// {
415-
417+
416418
// }
417419
//}
418420
}
@@ -619,9 +621,11 @@ private BuildDynamicAssemblyResult BuildDynamicAssembly(string assembleName, IGr
619621
IEnumerable<CreateIndex> create_indexes
620622
IEnumerable<DropIndex> drop_indexes
621623
*/
622-
//var t = typeof(ApiControllerAttribute);
623-
//tb.SetCustomAttribute(new CustomAttributeBuilder(t.GetConstructor(new Type[0]), new object[0]));
624-
624+
if (_addApiControllerAttribute)
625+
{
626+
var t = typeof(ApiControllerAttribute);
627+
tb.SetCustomAttribute(new CustomAttributeBuilder(t.GetConstructor(new Type[0]), new object[0]));
628+
}
625629

626630
//暂时取消登录验证 —— Jeffrey Su 2021.06.18
627631
//var t_0 = typeof(AuthorizeAttribute);

src/Senparc.CO2NET.WebApi/WebApiEngines/WebApiEngineOptions.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ public class WebApiEngineOptions
1919
/// <param name="defaultAction">默认请求类型,如 Post,Get</param>
2020
/// <param name="additionalAttributeFunc">额外需要绑定的特性</param>
2121
/// <param name="forbiddenExternalAccess">是否允许外部访问,默认为 false,只允许本机访问自动生成的 WebApi</param>
22-
23-
public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultRequestMethod = ApiRequestMethod.Post, Type baseApiControllerType = null, bool copyCustomAttributes = true, int taskCount = 4, bool showDetailApiLog = false, Func<MethodInfo, IEnumerable<CustomAttributeBuilder>> additionalAttributeFunc = null, bool forbiddenExternalAccess = true)
22+
/// <param name="addApiControllerAttribute">知否在自动生成的接口类(Controller)上自动添加 [ApiController] 标签</param>
23+
public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultRequestMethod = ApiRequestMethod.Post, Type baseApiControllerType = null, bool copyCustomAttributes = true, int taskCount = 4, bool showDetailApiLog = false, Func<MethodInfo, IEnumerable<CustomAttributeBuilder>> additionalAttributeFunc = null, bool forbiddenExternalAccess = true, bool addApiControllerAttribute = true)
2424
{
2525
DocXmlPath = docXmlPath;
2626
DefaultRequestMethod = defaultRequestMethod;
@@ -30,6 +30,7 @@ public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultReq
3030
ShowDetailApiLog = showDetailApiLog;
3131
AdditionalAttributeFunc = additionalAttributeFunc;
3232
ForbiddenExternalAccess = forbiddenExternalAccess;
33+
AddApiControllerAttribute = addApiControllerAttribute;
3334
}
3435

3536
/// <summary>
@@ -64,5 +65,6 @@ public WebApiEngineOptions(string docXmlPath = null, ApiRequestMethod defaultReq
6465
/// 是否允许外部访问,默认为 false,只允许本机访问自动生成的 WebApi
6566
/// </summary>
6667
public bool ForbiddenExternalAccess { get; set; }
68+
public bool AddApiControllerAttribute { get; }
6769
}
6870
}

0 commit comments

Comments
 (0)