You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

访问localhost:5001出现HTTP 404错误,求排查ASP.NET Core接口问题

问题排查与解决

错误原因

  1. 路由配置错误:控制器上的[Route("controller")]未使用方括号包裹占位符,导致路由被解析为字面量/controller,而非预期的控制器名称Courses。
  2. 访问路径不匹配:你访问的是根路径https://localhost:5001/,但当前控制器的正确接口路径并非根路径,导致404错误。

解决步骤

  1. 修正路由属性
    修改CoursesController的Route属性,将[Route("controller")]替换为[Route("[controller]")]。[controller]是ASP.NET Core的路由占位符,会自动替换为控制器类名去掉Controller后缀的部分(即Courses)。修正后的控制器代码如下:

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Threading.Tasks;
    using Microsoft.AspNetCore.Mvc;
    
    namespace CMS.WEBAPI.Controllers
    {
        [ApiController]
        [Route("[controller]")]    
        public class CoursesController: ControllerBase
        {
            public CoursesController()
            {
    
            }
            [HttpGet]
            public string GetCourses()
            {
                return "hello world";
            }
        }
    }
    
  2. 访问正确的接口地址
    启动项目后,访问https://localhost:5001/Courses,即可获取接口返回的hello world内容。

内容的提问来源于stack exchange,提问作者iBala

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 11:50:21