<Window x:Class="WapAPI.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:WapAPI"
mc:Ignorable="d"
WindowStartupLocation="CenterScreen"
Title="WebAPI服务" Height="300" Width="500"
Loaded="Window_Loaded">
<Grid>
<Grid.RowDefinitions>
<RowDefinition Height="50"/>
<RowDefinition Height="*" />
</Grid.RowDefinitions>
<StackPanel Orientation="Horizontal" HorizontalAlignment="Center" Grid.Row="0">
<Button Content="启动" Width="100" Background="Orange" Foreground="White" Height="40" Click="Button_Click"/>
<Button Content="停止" Width="100" Margin="20 0 0 0" Background="LightSkyBlue" Foreground="White" Height="40" Click="Button_Click_1"/>
</StackPanel>
<Grid Grid.Row="1">
<ListBox x:Name="listLog">
<ListBox.ItemTemplate>
<DataTemplate>
<TextBlock Text="{Binding}" TextWrapping="Wrap"/>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</Grid>
</Grid>
</Window>
?
public WebApiModule()
{
//主页
Get["/"] = _ =>
{
logger.Info("Received request: GET /");
return "Hello World!";
};
//get接收参数
Get["/api/{category}"] = parameters =>
{
return "My category is " + parameters.category;
};
//post接收和发送json信息
Post["/json"] = _ =>
{
Console.WriteLine(Request.Headers.Authorization); //Authorization
// 接收JSON数据
var json = this.Request.Body.AsString();
Console.WriteLine(json);
// 构建回复的JSON数据
var responseJson = new { Message = "Hello, World!" };
// 将响应转换为JSON字符串
string jsonResponse = JsonConvert.SerializeObject(responseJson);
// 返回JSON响应
return Response.AsText(jsonResponse, "application/json");
};
//返回文件流
Get["/api/file"] = parameters =>
{
//return Response.AsFile("C:\\config.ini");
string localFilePath = @"E:\Important\资料.xlsx";
FileStream fileStream = new System.IO.FileStream(localFilePath, FileMode.Open, FileAccess.Read);
return new StreamResponse(() => fileStream, MimeTypes.GetMimeType("资料.xlsx"));
};
}
?