C# ASP.NET Web Core API (.NET 5.0)

目录

一、创建项目

二、启动项目

三、开放其他电脑访问权限

1.launchSettings.json 

2.Program.cs 

四、调用接口

五、添加其他API


一、创建项目

新建一个 ASP.NET Web Core API 项目

二、启动项目

创建成功后,默认会有一个天气预报的控制器 WeatherForecastController.cs

启动调试后,会自动弹出这个界面,但这个不是我们想要的

直接在浏览器输入链接:https://localhost:44363/WeatherForecast

数据是 WeatherForecastController.cs 中的 Get方法 返回的:

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

到此为止,一个简单的接口就写好了,但此时其他的电脑访问会访问不到这个接口,下面就解决这个问题

三、开放其他电脑访问权限

1.launchSettings.json 

将 "applicationUrl" 改为 "http://0.0.0.0:5000",完整代码如下:

{
  "$schema": "http://json.schemastore.org/launchsettings.json",
  "iisSettings": {
    "windowsAuthentication": false,
    "anonymousAuthentication": true,
    "iisExpress": {
      "applicationUrl": "http://localhost:63035",
      "sslPort": 44395
    }
  },
  "profiles": {
    "IIS Express": {
      "commandName": "IISExpress",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "WebApplication2": {
      "commandName": "Project",
      "dotnetRunMessages": "true",
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "http://0.0.0.0:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    }
  }
}

2.Program.cs 

在 Program.cs 中加入下面代码:

webBuilder.ConfigureKestrel(options => options.ListenAnyIP(5000));

完整代码如下:

using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace WebApplication2
{
    public class Program
    {
        public static void Main(string[] args)
        {
            CreateHostBuilder(args).Build().Run();
        }

        public static IHostBuilder CreateHostBuilder(string[] args) =>
            Host.CreateDefaultBuilder(args)
                .ConfigureWebHostDefaults(webBuilder =>
                {
                    webBuilder.UseStartup<Startup>();
                    webBuilder.ConfigureKestrel(options => options.ListenAnyIP(5000));
                });
    }
}

四、调用接口

生成程序后,在debug目录下,启动程序

启动程序

用软件 Postman 调用接口,可以看到调用成功了

五、添加其他API

想加其他的接口,在Controllers文件夹下加就行了,控制器名字后面加个Controller

例子:

using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Logging;
using System;
using System.Collections.Generic;
using System.Linq;

namespace WebApplication2.Controllers
{
    [ApiController]
    [Route("[controller]")]
    public class MatchFingerprintController : ControllerBase
    {
        [HttpGet]
        public string Get()
        {
            return "指纹匹配";
        }

        [HttpPost]
        public string Post()
        {
            return "what fuck shit";
        }
    }
}

使用软件 Postman 调用  http://192.168.124.78:5000/MatchFingerprint 接口

这样就收到来自服务器的回复了

end

  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

熊思宇

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值