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

调用Get API返回404错误,请求存在哪些问题?

API请求404问题分析

问题根源

你的API方法路由配置与请求地址不匹配,导致服务器无法找到对应接口:

1. 第一个请求地址的问题

请求地址:http://localhost/api/myData?searchText=ex

  • 你的控制器应该标注了[Route("api/myData")](Web API控制器的常规配置),但GetMeData方法额外指定了[Route("searchText")],意味着该方法的完整路由是api/myData/searchText,而非直接挂载在api/myData下。直接访问api/myData会找不到对应处理方法,返回404。

2. 第二个请求地址的问题

请求地址:http://localhost/api/myData/searchText/ex

  • 方法参数searchText使用[FromUri]标记,表示参数通过查询字符串传递,但你将参数放在了路径中。当前方法的路由模板是searchText,未包含路径参数占位符,所以这个地址也无法匹配到方法。

解决方法

可根据需求选择以下两种方案:

方案一:保持查询字符串传参

使用正确的路由地址,参数通过查询字符串传递:

http://localhost/api/myData/searchText?searchText=ex

方案二:改为路径参数传参

修改API方法的路由模板,添加路径参数占位符([FromUri]可保留,也可省略,Web API默认支持路径参数绑定):

[HttpGet]
[Route("searchText/{searchText}")]
public HttpResponseMessage GetMeData([FromUri] string searchText)
{
    if (string.IsNullOrEmpty(searchText.Trim()))
        return Request.CreateResponse(HttpStatusCode.BadRequest);

    var response = myrepo.GetData(searchText);
    return Request.CreateResponse(HttpStatusCode.OK, response);
}

修改后即可使用以下地址访问:

http://localhost/api/myData/searchText/ex

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.07 12:15:10