优化了Web Api的使用

This commit is contained in:
fengjiayi
2024-10-10 17:24:45 +08:00
parent 99f82d5772
commit ef96b353ac
5 changed files with 21 additions and 34 deletions

View File

@@ -316,8 +316,14 @@ namespace Serein.Library.Web
{
return false;
}
var routeValues = GetUrlData(url); // 解析 URL 获取路由参数
if (!HandleModels.TryGetValue(httpMethod, out var modules))
{
return false;
}
if (!modules.TryGetValue(template, out var config))
{
return false;
}
ControllerBase controllerInstance = (ControllerBase)SereinIOC.Instantiate(controllerType);
@@ -326,47 +332,25 @@ namespace Serein.Library.Web
return false; // 未找到控制器实例
}
object invokeResult;
var routeValues = GetUrlData(url); // 解析 URL 获取路由参数
controllerInstance.Url = url.AbsolutePath;
if (!HandleModels.TryGetValue(httpMethod, out var modules))
{
return false;
}
if (!modules.TryGetValue(template,out var config))
{
return false;
}
dynamic invokeResult;
switch (httpMethod)
{
case "GET":
invokeResult = config.HandleGet(controllerInstance, routeValues);
invokeResult = await config.HandleGet(controllerInstance, routeValues);
break;
case "POST":
var requestBody = await ReadRequestBodyAsync(request); // 读取请求体内容
controllerInstance.BobyData = requestBody;
var requestJObject = JObject.Parse(requestBody);
invokeResult = config.HandlePost(controllerInstance, requestJObject, routeValues);
invokeResult = await config.HandlePost(controllerInstance, requestJObject, routeValues);
break;
default:
invokeResult = null;
break;
}
object result;
try
{
result = invokeResult.Result;
}
catch (Exception)
{
result = invokeResult;
}
Return(response, result); // 返回结果
Return(response, invokeResult); // 返回结果
return true;
}

View File

@@ -99,8 +99,8 @@
<Project>{73B272E8-222D-4D08-A030-F1E1DB70B9D1}</Project>
<Name>Serein.Library.Framework</Name>
</ProjectReference>
<ProjectReference Include="..\..\..\DynamicControl\SereinFlow\Library\Serein.Library.csproj">
<Project>{9FCE93C2-2278-46F3-96AB-CDAAFF27A55F}</Project>
<ProjectReference Include="..\Library\Serein.Library.csproj">
<Project>{5e19d0f2-913a-4d1c-a6f8-1e1227baa0e3}</Project>
<Name>Serein.Library</Name>
</ProjectReference>
</ItemGroup>

View File

@@ -49,7 +49,7 @@ namespace Net462DllTest
private void FromWorkBenchView_FormClosing(object sender, FormClosingEventArgs e)
{
ViewModel.CommandCloseForm?.Execute();
ViewModel.CommandCloseForm.Execute();
}
private void button2_Click(object sender, EventArgs e)

View File

@@ -132,6 +132,10 @@ namespace Net462DllTest.ViewModel
CommandGetParkingSpace = new RelayCommand((p) =>
{
viewManagement.TriggerSignal(SelectedSignal, SpcaeNumber);
});
CommandCloseForm = new RelayCommand((p) =>
{
});

View File

@@ -82,6 +82,7 @@ namespace Net462DllTest.Web
});
context.Env.IOC.Run<WebSocketServer>((socketServer) =>
{
socketServer.MsgHandleHelper.RemoteModule(this);
socketServer?.Stop(); // 关闭 Web 服务
});
MyPlc.Close();
@@ -105,8 +106,6 @@ namespace Net462DllTest.Web
string ip = "192.168.10.100",
int port = 102)
{
MyPlc.Model.Set(PlcVarName.DoorVar, (Int16)1);
MyPlc.Model.Get(PlcVarName.DoorVar);
if (MyPlc.Client is null)
{
try