提交 f39e8898 编写于 作者: 若汝棋茗

修复webapi在post时bug

上级 a8a9763b
using System;
using System.Threading.Tasks;
using TouchSocket.Core;
using TouchSocket.Core.Config;
using TouchSocket.Core.Log;
using TouchSocket.Core.Plugins;
using TouchSocket.Http;
using TouchSocket.Rpc;
......@@ -13,6 +15,14 @@ namespace WebApiServerApp
{
private static void Main(string[] args)
{
try
{
Enterprise.ForTest();
}
catch (Exception)
{
}
WebApiParserPlugin webApiParser=null;
HttpService service = new HttpService();
service.Setup(new TouchSocketConfig()
......@@ -39,6 +49,12 @@ namespace WebApiServerApp
public class Server : RpcServer
{
private readonly ILog m_logger;
public Server(ILog logger)
{
this.m_logger = logger;
}
[Origin(AllowOrigin ="*")]//跨域设置
[Router("[api]/[action]ab")]//此路由会以"/Server/Sumab"实现
[Router("[api]/[action]")]//此路由会以"/Server/Sum"实现
......@@ -68,6 +84,22 @@ namespace WebApiServerApp
}
return Task.FromResult("id不正确。");
}
/// <summary>
/// 使用调用上下文,获取实际请求体。
/// </summary>
/// <param name="callContext"></param>
[WebApi(HttpMethodType.POST, MethodFlags = MethodFlags.IncludeCallContext)]
[Router("[api]/[action]")]
public Task<string> PostContent(IWebApiCallContext callContext)
{
if (callContext.HttpContext.Request.TryGetContent(out byte[] content))
{
this.m_logger.Info($"共计:{content.Length}");
}
return Task.FromResult("ok");
}
}
public class MyClass
......
......@@ -200,14 +200,19 @@ namespace TouchSocket.Rpc.WebApi
{
try
{
int index;
ps = new object[methodInstance.Parameters.Length];
int i = 0;
if (methodInstance.MethodFlags.HasFlag(MethodFlags.IncludeCallContext))
{
ps[i] = callContext;
i++;
index = methodInstance.Parameters.Length - 2;
}
else
{
index = methodInstance.Parameters.Length - 1;
}
if (e.Context.Request.Query == null)
{
for (; i < methodInstance.Parameters.Length - 1; i++)
......@@ -231,8 +236,6 @@ namespace TouchSocket.Rpc.WebApi
}
}
int index = methodInstance.Parameters.Length - 1;
if (index >= 0)
{
string str = e.Context.Request.GetBody();
......
......@@ -4,7 +4,7 @@
<ApplicationIcon>logo.ico</ApplicationIcon>
<SignAssembly>True</SignAssembly>
<AssemblyOriginatorKeyFile>RRQM.pfx</AssemblyOriginatorKeyFile>
<Version>0.7.1</Version>
<Version>0.7.2</Version>
<LangVersion>8.0</LangVersion>
<Company>若汝棋茗</Company>
<Copyright>Copyright © 2022 若汝棋茗</Copyright>
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册