C#winform与web api联合,要如何写出以下要求呢?

需求:

1、他给我的接口发信息,我要如何获取他给我的数据呢?

2、我要返回给他的值如图并且用post,要怎么写代码呢?

img

3、域名用他给我提供的,我要如何改域名呢?

以下是我C#的代码,还是例子,不太懂,求指点
这是WeatherForecastController.cs

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;

namespace PopUpByMessage.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class WeatherForecastController : ControllerBase
    {
        private static readonly string[] Summaries = new[]
        {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

        private readonly ILogger<WeatherForecastController> _logger;

        public WeatherForecastController(ILogger<WeatherForecastController> logger)
        {
            _logger = logger;
        }

        [HttpGet(Name = "GetWeatherForecast")]
        public IEnumerable<WeatherForecast> Get()
        {
            return Enumerable.Range(1, 5).Select(index => new WeatherForecast
            {
                Date = DateTime.Now.AddDays(index),
                TemperatureC = Random.Shared.Next(-20, 55),
                Summary = Summaries[Random.Shared.Next(Summaries.Length)]
            })
            .ToArray();
        }
    }
}

这是StartUp.cs

using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Hosting;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Microsoft.OpenApi.Models;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace PopUpByMessage
{
    public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }

        public IConfiguration Configuration { get; }

        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {

            services.AddControllers();
            services.AddSwaggerGen(c =>
            {
                c.SwaggerDoc("v1", new OpenApiInfo { Title = "PopUpByMessage", Version = "v1" });
            });
        }

        // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
                app.UseSwagger();
                app.UseSwaggerUI(c => c.SwaggerEndpoint("/swagger/v1/swagger.json", "PopUpByMessage v1"));
            }

            app.UseRouting();

            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers();
            });
        }
    }
}


这是WeatherForecast.cs

using System;

namespace PopUpByMessage
{
    public class WeatherForecast
    {
        public DateTime Date { get; set; }

        public int TemperatureC { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public string Summary { get; set; }
    }
}


这是Form1的后端

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Hosting;

namespace PopUpByMessage
{
    public partial class Form1 : Form
    {
        private Lazy<IHost> webhost = new Lazy<IHost>(() => { return CreateHostBuilder().Build(); });
        //这里是直接搬的programs的代码,看着也眼熟对吧
        public static IHostBuilder CreateHostBuilder() =>
            Host.CreateDefaultBuilder()
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    //这里是新增的,我用Kestrel做http传输层,监听9527端口
                    webBuilder.UseKestrel(o => { o.ListenAnyIP(9527); });
                    webBuilder.UseStartup<Startup>();
                });
        public Form1()
        {
            InitializeComponent();
        }

        private async void button1_Click(object sender, EventArgs e)
        {
            await webhost.Value.StartAsync();
            MessageBox.Show("启动成功!");
        }
    }
}

你能说清楚点你的需求吗,好多个“他”,不知道你的”他“指的哪个项目了。

你是想要在winform项目传一些值到Web Api post方法里,然后web api post方法再返回一些值到你的winform项目里并获取response?

如果是的话,首先在winform里面,你可以使用HttpClient去调用Web Api并获取response,参考以下博客:

对于Web Api项目,你分享的代码只是创建ASP.NET Core Web Api默认生成的代码示例,看起来并不懂web api的样子,你可以参考以下博客:

Asp.Net Core Web Api的简单实例_纸墨青鸢的博客-CSDN博客_asp.net core webapi 文章目录WebApi第一个Asp.NetCoreWebApi程序WebApiWeb API是网络应用程序接口。包含了广泛的功能,网络应用通过API接口,可以实现存储服务、消息服务、计算服务等能力,利用这些能力可以进行开发出强大功能的web应用。第一个Asp.NetCoreWebApi程序首先新建项目,选择ASP.NET Core Web API项目名称随意就好直接点下一步就行新建好了是这样的Controllers文件夹是你的控制器WeatherForecastController. https://blog.csdn.net/qq_39935495/article/details/123123765?ops_request_misc=%257B%2522request%255Fid%2522%253A%2522164941010316780261971147%2522%252C%2522scm%2522%253A%252220140713.130102334.pc%255Fblog.%2522%257D&request_id=164941010316780261971147&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2~blog~first_rank_ecpm_v1~rank_v31_ecpm-6-123123765.nonecase&utm_term=asp.net+core+webapi

对于第三个问题,没太懂你这个跟改域名有什么联系,可以详细说一下。

winform 端你想与要以Post或者Get方式模拟http请求Api地址,请求的内容格式为Json格式。通过json反序列化,把数据展示在winform界面上

ApI返回格式默认是Json的 你只需要按照需要的返回的格式建一个类 在API方法中 返回这个类就OK了

我觉得你需要先去看一下WebApi的示例,这个默认的模板 返回的数据结构不是你想要的 自己要定义一个