本文介绍: C# .Net6后支持顶级语句,更简单的RestApi服务支持,可以快速搭建一个极为简洁的Web系统。推荐使用Visual Studio 2022,安装”ASP.NET 和Web开发”组件。

1、准备

C# .Net6后支持顶级语句,更简单的RestApi服务支持,可以快速搭建一个极为简洁的Web系统。推荐使用Visual Studio 2022,安装”ASP.NET 和Web开发”组件。

2、创建工程

     关键步骤如下:

包添加了“Newtonsoft.Json”,方便序列化和反序化。

3、工程代码


using Newtonsoft.Json;
using System.Runtime.CompilerServices;
using System.Text;

int bodySize = 8192;
Encoder encoder = Encoding.UTF8.GetEncoder();

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();

var app = builder.Build();

// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
    app.UseSwagger();
    app.UseSwaggerUI();
}

app.UseHttpsRedirection();

app.MapGet("/{*myUrl}", (HttpRequest request,string myUrl) => {
    return HttpRequestParse(request, myUrl);
});
app.MapPost("/{*myUrl}", (HttpRequest request, string myUrl) => {
    return HttpRequestParse(request, myUrl);
});

string HttpRequestParse(HttpRequest request, string myUrl)
{
    string body = "";
    using (var reader = new StreamReader(request.Body))
    {
        Task<string> t = reader.ReadToEndAsync();
        t.Wait();
        body = t.Result;
    }
    string msg = $"{myUrl} {request.Method} {request.Scheme} {request.Path} {request.QueryString} {body}";
    Console.WriteLine(msg);
    return msg;
}

app.Run();

代码中添加了Post和Get的全路径注册,并给出了如何解析请求数据和body数据的方法。返回值msg方面,建议返回一个Json对象,包含String Msg和int Code两个属性,方便应用使用。

4、部署文件

     编译之后,默认的启动端口是http://localhost:5000,可以通过修改appsettings.json配置文件实现其他绑定(“Kestrel”部分为新增)。

{
    "Logging": {
        "LogLevel": {
            "Default": "Information",
            "Microsoft.AspNetCore": "Warning"
        }
    },
    "AllowedHosts": "*",
    "Kestrel": {
        "Endpoints": {
            "Http": {
                "Url": "http://*:9000"
            }
        }
    }
}

也可以通过命令行指定,如:

dotnet AspNetCore.Pascal.dll –urls “http://localhost:7001;https://localhost:7011”

根据微软的官方说明,默认的 JsonConfigurationProvider 会按以下顺序加载配置:

  1. appsettings.json
  2. appsettings.{Environment}.json:例如,appsettings.Production.json 和 appsettings.Development.json 文件。 文件的环境版本是根据 IHostingEnvironment.EnvironmentName 加载的。 有关详细信息,请参阅在 ASP.NET Core 中使用多个环境

appsettings.{Environment}.json 值替代 appsettings.json 中的键。 例如,默认情况下:

  • 在开发环境中,appsettings.Development.json 配置会覆盖在 appsettings.json 中找到的值。
  • 在生产环境中,appsettings.Production.json 配置会覆盖在 appsettings.json 中找到的值。 例如,在将应用部署到 Azure 时。

如果必须保证配置值,请参阅 GetValue。 前面的示例只读取字符串,不支持默认值。

使用默认配置时,会通过 reloadOnChange: true 启用 appsettings.json 和 appsettings.{Environment}.json 文件。 应用启动后,对 appsettings.json 和 appsettings.{Environment}.json 文件所做的更改将由 JSON 配置提供程序读取。

5、参考资料

ASP.NET Core 中的配置

centos下dotnet服务启停脚本_centos dotnet 停止-CSDN博客

ASP.NET Core设置URLs的几种方法

最小 API 概述 | Microsoft Learn

原文地址:https://blog.csdn.net/yuming/article/details/135795377

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。

如若转载,请注明出处:http://www.7code.cn/show_62871.html

如若内容造成侵权/违法违规/事实不符,请联系代码007邮箱:suwngjj01@126.com进行投诉反馈,一经查实,立即删除!

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注