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

.Net Framework MVC项目Postman测API遇资源找不到错误

资源找不到错误的原因及解决办法

1. 请求URL写错了

你的Web API默认路由是api/{controller}/{id},控制器名是RegisterController,所以正确的POST请求URL应该是http://你的项目域名/api/Register,不需要加Action名Posthdf。Web API会自动根据HTTP POST方法匹配以Post开头的Action,如果你硬把Posthdf加到URL里,路由就匹配不上,自然报资源找不到。

2. Action命名与路由规则的可选调整

如果想明确指定Action的路由,可以给Action加上[Route]特性,手动绑定路由地址:

[HttpPost]
[Route("api/Register/Posthdf")] // 自定义路由
public IHttpActionResult Posthdf([FromBody] string data)
{
    if (!ModelState.IsValid) 
        return BadRequest("Date invalide");
    System.Diagnostics.Debug.WriteLine("***************************************EmailRegister " + data + "***************************************");
    return Ok("lalala");
}

这样请求URL就可以用http://你的项目域名/api/Register/Posthdf,但用默认路由的话,直接请求api/Register就能匹配到该Action。

3. 检查Web API路由是否正确注册

打开Global.asax文件,确认里面有注册WebApiConfig的代码:

protected void Application_Start()
{
    // 优先注册Web API路由,再注册MVC路由
    GlobalConfiguration.Configure(WebApiConfig.Register);
    AreaRegistration.RegisterAllAreas();
    RouteConfig.RegisterRoutes(RouteTable.Routes);
}

如果没加GlobalConfiguration.Configure(WebApiConfig.Register);这行代码,Web API的路由规则根本不会生效,必然出现资源找不到的错误。

4. 请求体格式的注意事项(后续测试需留意)

你的Action参数是[FromBody] string data,在Postman中发送请求时要注意:

  • 选择raw格式,文本类型选Text或JSON
  • 如果传纯字符串,直接写内容即可;如果用JSON格式,要给字符串加引号,比如"test@example.com"
  • Web API对单个字符串的[FromBody]解析有特殊规则,也可以写成=你的字符串(例如=test@example.com)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.17 16:45:42