[Route("api/[controller]")] public class MyController : ControllerBase { [HttpGet("{id}")] public ActionResult Get(int id) { // implement the method } }
确认项目中是否添加了Microsoft.AspNetCore.Mvc.Core NuGet 包,它包含了 ASP.NET Core 中的基本 MVC 功能。如果没有添加该包,请在项目中添加并重新构建。
若仍然出现404错误,可能是应用程序中缺少了适当的路由规则。使用app.UseEndpoints方法添加适当的端点配置,为API操作提供适当的路由规则。示例代码如下:
app.UseEndpoints(endpoints => { endpoints.MapControllers(); });
通过以上解决方法,可以解决ASP.NET Core Web Api控制器端点返回404错误的问题。