文章
问答
冒泡
Ⅰ .net core 6 基础工程搭建

由于业务需要,开始熟悉.net core相关技术体系。这里,从一个最基础的.net core工程开始。
按照visual studio的过程就不赘述了,直接从创建工程开始。

按照步骤,创建
0
 
0
 
0
 
创建完成之后,可以看进入工作界面。
0
 
运行项目,会跳转到swagger界面
0
 

从目录结构可以看出
appsettings.json 是配置文件
Program.cs 是入口函数
Controllers 中就是控制器了

appsettings.json 中主要是一些基础配置文件

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*"
}


Program.cs 是启动类,里面可以看到要启用哪些组件这里有  Swagger,SwaggerUI,Authorization,MapControllers

var builder = WebApplication.CreateBuilder(args);

// Add services to the container.

builder.Services.AddControllers();
// 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.UseAuthorization();

app.MapControllers();

app.Run();

WeatherForecastController.cs 文件作为一个Controller ,这里的表示意思是 使用controller模板,也就是以WeatherForecast作为路径

using Microsoft.AspNetCore.Mvc;

namespace book.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();
        }
    }
}

由于使用习惯,还是喜欢自己去定义路径,我们可以按照spring mvc的方法来操作

namespace book.Controllers
{
    [ApiController]
    [Route("weather-forecasts")]
    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("all",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();
        }

    }
}

 

0

 

一个最基础的.net core api项目已经创建完成

.net core 6

关于作者

落雁沙
非典型码农
获得点赞
文章被阅读