.net 6 api 修改URL为小写

我们创建的api项目,url是[Route("[controller]")],类似这样子定义的。我们的controller命名是大写字母开头的,显示在url很明显不是很好看(url不区分大小写)。转换方式:

csharp 复制代码
var builder = WebApplication.CreateBuilder(args);
路由以小写形式展示
builder.Services.AddRouting(options =>
{
    options.LowercaseUrls = true;
});
var app = builder.Build();
app.Run();

配置好以上代码后,展示的路由地址就会以小写显示,但是我们实际访问时也可根据大写去访问

csharp 复制代码
namespace Test.Host.Controllers.Management
{
    [Route("BasicInformation")]
    [ApiController]
    [Authorize]
    public class BasicInformationController : ControllerBase
    {
        private readonly ICurrentUserService _currentUser;
        public BasicInformationController(ICurrentUserService currentUser)
        {
            _currentUser = currentUser;
        }

        /// <summary>
        /// 获取列表
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        [HttpGet("List")]
        public async Task<dynamic> GetListAsync([FromQuery] GetInput input)
        {
            return new { totalCount = 100, items = null};
        }

        /// <summary>
        /// 根据指定字段获取详情
        /// </summary>
        /// <param name="value"></param>
        /// <returns></returns>
        [HttpGet("IdCard/{value}")]
        public async Task<ActionResult> GetByIdCardAsync(string value)
        {
            return Ok("haha");
        }
}