ASP .NET Core Web API中的路由中使用点号导致404未找到
创始人
2024-11-11 14:02:08
0

在ASP .NET Core Web API中,如果在路由中使用点号导致404未找到错误,可以通过以下几种方法进行解决:

  1. 在Startup类的ConfigureServices方法中,添加以下代码:
services.AddControllers(options =>
{
    options.SuppressAsyncSuffixInActionNames = false;
    options.AllowEmptyInputInBodyModelBinding = true;
}).AddNewtonsoftJson();

这将允许路由中使用点号,并使用NewtonsoftJson作为默认的JSON序列化器。

  1. 在路由属性上使用[HttpGet("{id}", Name = "GetById")],并在控制器的方法中返回CreatedAtRoute
[HttpGet("{id}", Name = "GetById")]
public IActionResult GetById(int id)
{
    // 从数据库中获取数据
    var data = _repository.GetById(id);

    if (data == null)
    {
        return NotFound();
    }

    return CreatedAtRoute("GetById", new { id = data.Id }, data);
}

这将在返回的响应头中包含Location头,其中包含URL路径,该路径将匹配带有点号的路由。

  1. 使用URL编码代替点号。如果路由中的点号是作为文件扩展名的一部分,可以将点号编码为"%2E"。例如,路由可以写为[HttpGet("file/{name}.{extension}")],并且可以通过/file/myfile%2Etxt访问。

通过使用上述方法之一,你可以在ASP .NET Core Web API中的路由中使用点号,而不会导致404未找到错误。

相关内容

热门资讯

透视脚本!wepoker分析,... 透视脚本!wepoker分析,wejoker辅助软件价格(详细到底是挂了)1、下载好wejoker辅...
透视安卓版!aapoker透视... 透视安卓版!aapoker透视脚本下载(透视)aapoker免费透视脚本,必赢方法(有挂规律)1、实...
透视玄学!wepoker可以开... 透视玄学!wepoker可以开透视吗,wepoker免费脚本(详细作弊方法)1、wepoker可以开...
透视ai代打!aapoker脚... 透视ai代打!aapoker脚本(透视)aapoker脚本怎么用,玩家教你(有挂脚本)1)aapok...
透视ai代打!wepoker辅... 透视ai代打!wepoker辅助器下载,wepoker亲友圈有用吗(详细可以破解游戏)1、透视ai代...
透视ai!aapoker真的假... 透视ai!aapoker真的假的(透视)aapoker怎么设置抽水,细节揭秘(有挂解密)1、操作简单...
透视代打!wepoker破解器... 透视代打!wepoker破解器有用吗,wepoker怎么增加运气(详细轻量版透视系统);wepoke...
透视总结!aapoker破解侠... 透视总结!aapoker破解侠是真的吗(透视)aapoker怎么控制牌,微扑克教程(有挂介绍)1、上...
透视教学!wepoker怎么提... 透视教学!wepoker怎么提高运气,wejoker辅助器怎么卖(详细开挂)一、wejoker辅助器...
透视线上!aapoker怎么设... 透视线上!aapoker怎么设置抽水(透视)aapoker辅助器是真的吗,技巧教程(有挂技巧)1、完...