【问题标题】:.NET Core Web Api: call localhost returns 404.NET Core Web Api:调用 localhost 返回 404
【发布时间】:2019-02-05 08:34:34
【问题描述】:
我有一个 .NET Core Web Api,当我使用 IIS Express 启动项目时,我得到一个 404,
当我使用 Postman 并调用 web Api-localhost(没有路由)时,我得到了 500。
但在我的情况下,我得到一个 404,有或没有路由。
在 launch.settings 我得到了:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:63793",
"sslPort": 0
}
为什么我在拨打“http://localhost:63793”时会收到 404?
【问题讨论】:
标签:
c#
asp.net-core
localhost
asp.net-core-webapi
【解决方案1】:
既然您已经提供了控制器,我相信您只是访问了错误的 URL。
[Route("graph/[controller]")]
public class GraphController : ControllerBase
这意味着您可能应该访问graph/graph/users
这里的控制器部分 ["graph/[controller"] 将获得与您的控制器名称相同的名称,例如 GraphController。一个简单优雅的解决方案是将第一部分重命名为 api/[controller] --> 然后 url 将是 baseurl:port/api/graph/route 或者在你的情况下:localhost:63793/api/graph/users
试试看。
【解决方案2】:
为什么我在拨打“http://localhost:63793”时会收到 404?
似乎是因为您在此地址上没有资源。
localhost:63793/graph/用户
根据您的路由属性[Route("graph/[controller]")] 和[Route("Users")],您的资源位于localhost:63793/graph/graph/users,因为您有一个前缀graph/,并且您的控制器名为GraphController。通常 api url 有 api/ 前缀。