我有一个 .NET Core Web Api ,当我使用IIS Express启动项目时,我得到了404, 当我使用邮递员并呼叫Web Api本地主机(无路由)时,得到500。 但就我而言,无论有没有路线,我都会得到404。 在launch.settings中,我得到了:
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:63793",
"sslPort": 0
}
为什么我打“ http://localhost:63793”时得到404?
答案 0 :(得分:1)
现在您已经提供了控制器,我相信您只是在访问错误的URL。
[Route("graph/[controller]")]
public class GraphController : ControllerBase
这意味着您可能应该访问graph/graph/users
此处["graph/[controller"]
的控制器部分将获得与您的控制器名称相同的名称,例如 Graph Controller。一个简单而优雅的解决方案是将第一部分重命名为api/[controller]
->则URL将为baseurl:port/api/graph/route
或您的情况:localhost:63793/api/graph/users
尝试一下。
答案 1 :(得分:0)
[Route("graph/[controller]")]
public class GraphController : ControllerBase
{
/// <summary>
/// Retrieve configured AAD Users
/// </summary>
/// <returns>An awaitable <seealso cref="Task{ICollection{AADUser}}>}"/>that contains a mapping of Ids of Users and their mapped Groups</returns>
[Route("Users")]
[HttpGet]
public async Task<IActionResult> GetUsersAsync()
{
ICollection<AADUser> response;
try
{
response = await graphBC.GetUsersAsync().ConfigureAwait(false);
if (response == null || response.Count < 1)
{
return NotFound();
}
}
catch (Exception ex)
{
throw ex;
}
return Ok(response);
}
当我在shell上调用它时,会得到返回:
curl -v http://localhost:63793/graph/Users
* Trying ::1...
* TCP_NODELAY set
* Connected to localhost (::1) port 63793 (#0)
> GET /graph/Users HTTP/1.1
> Host: localhost:63793
> User-Agent: curl/7.55.1
> Accept: */*
>
< HTTP/1.1 404 Not Found
< Server: Kestrel
< X-SourceFiles: =?UTF-8?B?Qzpcd29ya3NwYWNlXEJFLWFhZC1uZXRjb3JlXExFT05JU21hcnRDYWJsZS5BQURcTEVPTklTbWFydENhYmxlLkFBRC5Db3JlXGdyYXBoXFVzZXJz?=
< X-Powered-By: ASP.NET
< Date: Tue, 05 Feb 2019 09:24:35 GMT
< Content-Length: 0
<
* Connection #0 to host localhost left intact
答案 2 :(得分:0)
为什么我打“ http://localhost:63793”时得到404?
似乎是因为您在此地址上没有资源。
localhost:63793 / graph / Users
根据路由属性[Route("graph/[controller]")]
和[Route("Users")]
,您的资源位于localhost:63793/graph/graph/users
上,因为您有前缀graph/
,并且控制器名为GraphController
。通常api网址的前缀为api/
。